2
List<String[]> allWordList = new ArrayList<>();

I would like to sort the "allWordList" list based on the first element in string array alphabetically.

I have a list that contains string array which are of size 2. So basically i want to sort this list by comparing the first element of the string array.

Collection.sort();

does not work as it is used to sort......

List<String>

and not

List<String[]>

to be clear i do not want to sort the individual string[] elements. I would like to sort the entire list based on the very first element of the string array.

vivek verma
  • 1,716
  • 1
  • 17
  • 26
  • 1
    Possible duplicate of [Sorting arraylist in alphabetical order (case insensitive)](http://stackoverflow.com/questions/5815423/sorting-arraylist-in-alphabetical-order-case-insensitive) – Arthur Aug 20 '16 at 22:09
  • @Arrthur: Not a duplicate I have string[] inside a list whereas the other question has just strings – vivek verma Aug 20 '16 at 22:32

5 Answers5

6

A simple custom comparator should do the trick.

The only tricky thing is making sure that you are not indexing into an empty array:

Collections.sort(allWordList, new Comparator<String[]>() {
    public int compare(String[] o1, String[] o2) {
        if (o1.length == 0) {
            return o2.length == 0 ? 0 : -1;
        }
        if (o2.length == 0) {
            return 1;
        }
        return o2[0].compareTo(o1[0]);
    }
});
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • does not work says the first argument is wrong>>Found : List, Required: String[][] – vivek verma Aug 20 '16 at 22:26
  • @vivekverma You are right, this should be `Collections.sort`. Thank you! – Sergey Kalinichenko Aug 20 '16 at 22:28
  • 1
    Why are you checking the array size and tolerate empty arrays? The arrays should have a size of 2. – Jimmy T. Aug 20 '16 at 22:46
  • *"sort this list by comparing the **first element** of the string array"*. Without further description, it is an error for the array to be `null` or empty, same as it is an error for the first element to be `null`. Both `null` and empty means "missing" and *should fail!* If you want to invent sorting rule for missing element (empty array), you might as well invent sorting rules for null too. – Andreas Aug 20 '16 at 23:03
2

You can provide your own comparator, for example as a lambda expression:

allWordList.sort((o1, o2) -> o1[0].compareTo(o2[0]));
msumera
  • 160
  • 1
  • 9
  • 1
    Since values are string arrays, maybe `s1` (for "string") or `a1` (for "array") is better than `o1` (for "object"). – Andreas Aug 20 '16 at 23:00
0

I'd go with a variation of @dasblinkenlight 's answer where I'd don't think comparing array length's is correct behavior.

private void sort() {
    List<String[]> allWordList = new ArrayList<String[]>();
    // fill the list...
    Collections.sort(allWordList, new FirstElementComparator());
    // and so on...
}

private static class FirstElementComparator implements Comparator<String[]>
{
    @Override
    public int compare(String[] sa1, String[] sa2) {
        String str1 = sa1[0];
        String str2 = sa2[0];

        int result = 0;
        if (str1 == null) {
            result = (str2 == null) ? 0 : -1;
        }
        else {
            result = str1.compareTo(str2);
        }
        return result;
    }
}
pamcevoy
  • 1,136
  • 11
  • 15
0

You don't need to write your own Comparator:

allWordList
    .stream()
    .sorted(Comparator.comparing(a -> a[0], String.CASE_INSENSITIVE_ORDER))
    .collect(Collectors.toList());

You can omit the String.CASE_INSENSITIVE_ORDER if the ordering should be case-sensitive.

Or if you need locale-dependent ordering, take a look at Collator.

xehpuk
  • 7,814
  • 3
  • 30
  • 54
-3

Check This :

Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    });

Source from here

Option 2 :

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

Source from here

Community
  • 1
  • 1
Phoenix
  • 467
  • 1
  • 11
  • 23