Sort ArrayList of Array in Java

后端 未结 7 1089
感动是毒
感动是毒 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 like so:

    new Comparator() {
      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.

提交回复
热议问题