What is the best way to sort an ArrayList
in Java?
Where String[] is...
String[] = new String[] { \"abc\", \"abc\", \"ab
You create a Comparator<String[]>
like so:
new Comparator<String[]>() {
public int compare(String[] first, String[] second) {
return first[1].compareTo(second[1]);
}
}
then pass it to Collections.sort()
.
You might want to do some checking if the second element is actually present in the array. You could also do a custom comparison if the standard String comparison isn't enough.