Results 1 to 6 of 6
  1. #1
    solariums is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Sort a list alphabetically and split it into groups (From A to E, From F to J....)

    Hi,

    I have an arraylist which I have to sort in an alphebetical order then I have to split it into 5 sets
    - set 1 containing list of words between A and E
    - set 2 containing list of words between F and J
    - set 3 containing list of words between K and O
    - set 4 containing list of words between P and T
    - set 5 containing list of words between U and Z

    Also, there are some elements in the arraylist which start with a number, I have to have another set
    - set 6 containing list of between 0 and 9



    Can anyone help please???

    Thanks in advance

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Sort a list alphabetically and split it into groups (From A to E, From F to J....

    What have you tried? Where are you stuck? How would you do this by hand, without a computer?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    solariums is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Sort a list alphabetically and split it into groups (From A to E, From F to J....

    Thanks for your reply. I can sort the list alphabetically using the collection interface on List
    ArrayList<String> ParamList = new ArrayList<String>();
    Now what I want is a way to filter the list. I would like to create sublists of alphabet by spliting the 26 letters of the alphabet into 5 sublists
    From A and E contains all strings starting with A, B, C, D or E
    From F and J contains all strings starting with F, G, H, I or J
    From K and O contains all strings starting with K, L, M, N or O
    From P and T contains all strings starting with P, Q, R, S or T
    From U and Z contains all strings starting with U, V, W, X, Y or Z

    This is to allow easier searh, instead of displaying the whole list of data, I first want to filter in order to avoid displaying a long list.

  4. #4
    solariums is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Sort a list alphabetically and split it into groups (From A to E, From F to J....

    One way of doing this could be to declare 5 sublists and state that if for example:
    a string starts with letter "A" then place it in list 1,
    String starts with letter "G" then place it in list 2....

    Or, we can create 26 lists, a list for each letter in the alphabet, then put its content if not empty in a new list


    Thanks

  5. #5
    solariums is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Sort a list alphabetically and split it into groups (From A to E, From F to J....

    In a clearer way:

    I have a list of strings some begin with a number (eg. 6Kv, 9Hv) and some begin with a letter from the alphabet (eg. Aa, Clapet, Joint...)

    I want to filter the data and create 6 sublists
    List1 : contains all strings beginning with a number sorted from 0 to 9
    List2 : contains all strings beginning with A, B, C, D, E
    List3 : contains all strings beginning with F, G, H, I, J
    List4 : contains all strings beginning with K, L, M, N, O
    List5 : contains all strings beginning with U, V, W, X, Y, Z


    Please, HELP

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Sort a list alphabetically and split it into groups (From A to E, From F to J....

    Right, so this is simple. Create your 6 lists. Iterate through the main list (after it has been sorted). Use the String class' beginsWith() method or the charAt() to see what the string starts with. If it begins with A, B, C, D or E, put it in list one. Repeat for each remaining item into its corresponding bucket. A loop and a switch statement should do this easily.

Similar Threads

  1. need help to sort a list
    By gretaks in forum New To Java
    Replies: 13
    Last Post: 11-02-2011, 10:07 PM
  2. Sort by Word frequency and alphabetically
    By darpan12 in forum New To Java
    Replies: 3
    Last Post: 01-06-2011, 06:26 PM
  3. Arraylist problem Sort Date , alphabetically
    By ob3lix in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 03:55 PM
  4. how to sort jtable with row groups
    By mordjah in forum AWT / Swing
    Replies: 0
    Last Post: 10-25-2010, 05:23 PM
  5. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •