java 排序 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15666382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:27:04  来源:igfitidea点击:

Sorting ArrayList

javasortingarraylist

提问by Klausos Klausos

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.

由于我刚刚开始使用 JAVA,我很好奇在 JAVA 中实现排序的最佳选择是什么(对于 ArrayLists)。下面我提供我的PHP代码。

public int cmp($a, $b) {
    if ( $a[0] < $b[0] ) return 1;
    else if ( $a[0] > $b[0] ) return -1;
    else if ( $a[1] < $b[1] ) return 1;
    else if ( $a[1] > $b[1] ) return -1;
    else return 0;
}

$selected = array();

for ($i=0; $i<$len; $i++) {
    $rank = getRank();
    $cub = getCub_len();
    $selected[] = array($rank,$cub);
}

uasort($selected, 'cmp')

Well, I wrote the following code in JAVA:

好吧,我用JAVA写了以下代码:

ArrayList<ArrayList<Double>> selected = new ArrayList<ArrayList<Double>>();
ArrayList<Double> rank = new ArrayList<Double>();
ArrayList<Double> cub = new ArrayList<Double>();

for (int i=0; i<len; i++) {
 rank.add(getRank(i));
 cub.add(getCub(i));
}

selected.add(0,rank);
selected.add(1,cub);

How to sort selectedin the proper way (similarly to PHP function cmp)?

如何以selected正确的方式排序(类似于 PHP 函数cmp)?

回答by Chauer

Collections.sort(a);

Sources: Sorting an ArrayList

来源:对 ArrayList 进行排序

回答by Alya'a Gamal

Try this Way on you example :

在你的例子上试试这种方式:

public static void main(String[] args) throws Exception {
        ArrayList<String[]> listOfStringArrays = new ArrayList<String[]>();
        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<String[]>() {
            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]
        */ 

    }

回答by Sajad

Try this solution:

试试这个解决方案:

public class SortArrayList{
    public static void main(String args[]){

        List<String> unsortList = new ArrayList<String>();

        unsortList.add("CCC");
        unsortList.add("111");
        unsortList.add("AAA");
        unsortList.add("BBB");
        unsortList.add("ccc");
        unsortList.add("bbb");
        unsortList.add("aaa");
        unsortList.add("333");
        unsortList.add("222");

        //before sort
        System.out.println("ArrayList is unsort");
        for(String temp: unsortList){
            System.out.println(temp);
        }

        //sort the list
        Collections.sort(unsortList);

        //after sorted
        System.out.println("ArrayList is sorted");
        for(String temp: unsortList){
            System.out.println(temp);
        }
    }
}

回答by macmuri

for(int j = 0; j < myArray.size(); j++) {
        for (int i = j+1 ; i < myArray.size(); i++){
            if(myArray.get(i)[2].compareTo(myArray.get(j)[2]) < 0){
                String[] temp = myArray.get(j);
                myArray.set(j, myArray.get(i)); 
                myArray.set(i, temp); 
            }
        }
    }

I'm using the third field (myArray.get(j)[2])for comparing. I hope this will help someone.

我正在使用第三个字段(myArray.get(j)[2])进行比较。我希望这会帮助某人。

回答by Rodrigo Sasaki

The simplest way to implement sorting in Java Collections is using the Collections#sortmethod.

在 Java 集合中实现排序的最简单方法是使用Collections#sort方法。

It uses a modified merge sortalgorithm to do the job.

它使用修改后的合并排序算法来完成这项工作。

It is important to say that it can only sort objects of a class that implements the Comparableinterface, so you may need to take that into account. When you implement this interface you should know that it is best to think about the natural ordering of the object in question. e.g. Alphabetic order for Strings, if you need to sort it in an unnatural way on a specific context, do not use this interface.

重要的是要说它只能对实现Comparable接口的类的对象进行排序,因此您可能需要考虑到这一点。当您实现此接口时,您应该知道最好考虑所讨论对象的自然顺序。例如字符串的字母顺序,如果您需要在特定上下文中以不自然的方式对其进行排序,请不要使用此接口。

For that it's best if you define a Comparatorwhen you invoke the method.

为此,最好在调用方法时定义一个Comparator

回答by Bizmarck

The way to do this would be to use Collections class, and call Collections.sort(java.util.List, java.util.Comparator)on your list. It is documented here. The Comparator here is an interface that you would need to implement in order to do a custom sort. To implement the Comparatorinterface you need to provide implementations for

这样做的方法是使用 Collections 类,并调用Collections.sort(java.util.List, java.util.Comparator)您的列表。它记录在此处。这里的 Comparator 是一个接口,您需要实现它才能进行自定义排序。要实现Comparator接口,您需要提供实现

int compare(T o1,T o2)and boolean equals(Object obj).

int compare(T o1,T o2)boolean equals(Object obj)

using the logic you already have in your PHP file.

使用您在 PHP 文件中已有的逻辑。