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

1. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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

2. ## 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?

3. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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

6. ## 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.