Sorting ArrayList

后端 未结 6 964
我在风中等你
我在风中等你 2021-01-25 19:19

Since I\'m just starting with JAVA, I\'m curious what is the best option for implementing sorting in JAVA (for ArrayLists). Below I provide my PHP code.

public i         


        
6条回答
  •  情歌与酒
    2021-01-25 20:04

    Try this Way on you example :

    public static void main(String[] args) throws Exception {
            ArrayList listOfStringArrays = new ArrayList();
            listOfStringArrays.add(new String[] {"x","y","z"});
            listOfStringArrays.add(new String[] {"a","b","c"});
            listOfStringArrays.add(new String[] {"m","n","o"});
            Collections.sort(listOfStringArrays,new Comparator() {
                public int compare(String[] strings, String[] otherStrings) {
                    return strings[1].compareTo(otherStrings[1]);
                }
            });
            for (String[] sa : listOfStringArrays) {
                System.out.println(Arrays.toString(sa));
            }
            /* prints out 
              [a, b, c]
              [m, n, o]
              [x, y, z]
            */ 
    
        }
    

提交回复
热议问题