36

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.

hichris123
  • 10,145
  • 15
  • 56
  • 70
antz
  • 1,031
  • 2
  • 9
  • 17
  • 2
    Use Collections.sort() with subList() – Peter Lawrey Oct 24 '12 at 09: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... – Andrea Ligios Oct 24 '12 at 09: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 – antz Oct 24 '12 at 09:42

5 Answers5

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

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

Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • 1
    You need to delcare the sublist outside the sort to get the results: ``List sublist = teamsName.subList(1, teamsName.size()); Collections.sort(sublist);`` – Jean Logeart Oct 24 '12 at 09:45
  • 1
    Shouldn't the `fromIndex` in the subList be a 0 instead of a 1? – dokaspar Jun 01 '15 at 05:56
  • 1
    @dokaspar No. The OP expressly wants to exclude the first element. Read the question - specifically at the end. – rayryeng Dec 02 '16 at 06:30
  • OP's request can create some misunderstanding when copying and pasting.. As question looks more generic... – Hayra Aug 21 '18 at 09:13
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);
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
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.

npinti
  • 51,780
  • 5
  • 72
  • 96
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)

petrsyn
  • 5,054
  • 3
  • 45
  • 48
2

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'.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
katsu
  • 558
  • 1
  • 7
  • 20
  • `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. – bronzenose Dec 11 '20 at 17:17