What is the best way to sort an ArrayList in Java?
Where String[] is...
String[] = new String[] { \"abc\", \"abc\", \"ab
This is extremely easy to do with Java 8. Just write:
list.sort(Comparator.comparing(a -> a[1]));
For example, the following code:
List list = Arrays.asList(
new String[] { "abc", "abc", "abc", "abc", "abc", "abc", "abc" },
new String[] { "xyz", "xyz", "xyz", "xyz", "xyz", "xyz", "xyz" },
new String[] { "fgh", "fgh", "fgh", "fgh", "fgh", "fgh", "fgh" });
list.sort(Comparator.comparing(a -> a[1]));
list.stream().map(Arrays::toString).forEach(System.out::println);
Will yield the wanted result:
[abc, abc, abc, abc, abc, abc, abc]
[fgh, fgh, fgh, fgh, fgh, fgh, fgh]
[xyz, xyz, xyz, xyz, xyz, xyz, xyz]