Sort ArrayList of Array in Java

后端 未结 7 1074
感动是毒
感动是毒 2020-11-29 09:20

What is the best way to sort an ArrayList in Java?

Where String[] is...

String[] = new String[] { \"abc\", \"abc\", \"ab         


        
相关标签:
7条回答
  • 2020-11-29 10:20

    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.

    0 讨论(0)
提交回复
热议问题