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.

2

5 Answers 5

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]);
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

does not work says the first argument is wrong>>Found : List<String[]>, Required: String[][]
@vivekverma You are right, this should be Collections.sort. Thank you!
Why are you checking the array size and tolerate empty arrays? The arrays should have a size of 2.
"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.
2

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

allWordList.sort((o1, o2) -> o1[0].compareTo(o2[0]));

1 Comment

Since values are string arrays, maybe s1 (for "string") or a1 (for "array") is better than o1 (for "object").
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;
    }
}

Comments

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.

Comments

-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

1 Comment

You realize OP has a String array?

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.