37

I have Strings that are put into an ArrayList randomly.

private ArrayList<String> teamsName = new ArrayList<String>();
String[] helper; 

For example:

teamsName.add(helper[0]) where helper[0] = "dragon";   
teamsName.add(helper[1]) where helper[1] = "zebra";   
teamsName.add(helper[2]) where helper[2] = "tigers" // and so forth up to about 150 strings.

Given the fact that you cannot control the inputs (i.e. string that is coming into the ArrayList is random; zebra or dragon in any order), once the ArrayListis filled with inputs, how do I sort them alphabetically excluding the first one?

teamsName[0] is fine; sort teamsName[1 to teamsName.size] alphabetically.

3
  • 2
    Use Collections.sort() with subList() Commented Oct 24, 2012 at 9:39
  • and btw why is first element excluded ? I hope it's not because first element is the header element like 'Team name'... that should be managed in frontend only, and filtered later... Commented Oct 24, 2012 at 9:41
  • first element is the name of my team so I would like to have that as first; it will be on the drop down menu so like how we have United States as the first and alphabetize the rest of the countries Commented Oct 24, 2012 at 9:42

5 Answers 5

44
Collections.sort(teamsName.subList(1, teamsName.size()));

The code above will reflect the actual sublist of your original list sorted.

Sign up to request clarification or add additional context in comments.

4 Comments

You need to delcare the sublist outside the sort to get the results: List<String> sublist = teamsName.subList(1, teamsName.size()); Collections.sort(sublist);
Shouldn't the fromIndex in the subList be a 0 instead of a 1?
@dokaspar No. The OP expressly wants to exclude the first element. Read the question - specifically at the end.
OP's request can create some misunderstanding when copying and pasting.. As question looks more generic...
12

Check Collections#sort method. This automatically sorts your list according to natural ordering. You can apply this method on each sublist you obtain using List#subList method.

private List<String> teamsName = new ArrayList<String>();
List<String> subList = teamsName.subList(1, teamsName.size());
Collections.sort(subList);

Comments

6

Take a look at the Collections.sort(List<T> list).

You can simply remove the first element, sort the list and then add it back again.

Comments

5

You might sort the helper[] array directly:

java.util.Arrays.sort(helper, 1, helper.length);

Sorts the array from index 1 to the end. Leaves the first item at index 0 untouched.

See Arrays.sort(Object[] a, int fromIndex, int toIndex)

Comments

3

You can use TreeSet that automatically order list values:

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

    public static void main(String[] args) {
        System.out.println("Tree Set Example!\n");

        TreeSet <String>tree = new TreeSet<String>();
        tree.add("aaa");
        tree.add("acbbb");
        tree.add("aab");
        tree.add("c");
        tree.add("a");

        Iterator iterator;
        iterator = tree.iterator();

        System.out.print("Tree set data: ");

        //Displaying the Tree set data
        while (iterator.hasNext()){
            System.out.print(iterator.next() + " ");
        }
    }

}

I lastly add 'a' but last element must be 'c'.

1 Comment

TreeSet is definitely handy but a set is not a list. Using TreeSet will remove duplicates which may be unwanted: the question is about sorting, not sorting and de-duping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.