java Collections.sort 不排序任何东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28171695/
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-11-02 13:07:39  来源:igfitidea点击:

Collections.sort is not sorting anything

javasortingcollections

提问by Berit Larsen

I'm trying to sort a String array in a short, simple way. I'm trying to use Collections.sort, but I don't understand why it doesn't sort anything. Code:

我正在尝试以一种简短的方式对 String 数组进行排序。我正在尝试使用 Collections.sort,但我不明白为什么它不对任何内容进行排序。代码:

public static String[] FishNamesSorted;
.....
List<String> nameslist = new ArrayList<String>();
nameslist.toArray(FishNamesSorted);
Collections.sort(nameslist, String.CASE_INSENSITIVE_ORDER); <--- NOT WORKING

Collections.sort(nameslist, new Comparator<String>() { <--- NOT WORKING
    @Override
    public int compare(String p1, String p2) {
    if (p1 == null) {
        return 1;
    }
    if (p2 == null) {
        return -1;
    }
    return p1.compareToIgnoreCase(p2);
    }
});

Results in both cases:

两种情况下的结果:

  • Poecilia Latipinna
  • Poecilia Reticulata
  • Notropis Chrosomus
  • Pseudomugil Gertrudae
  • ....
  • 波西莉亚·拉蒂皮娜
  • 铁线莲
  • 向日葵
  • 假木虱科
  • ....

Whyyyy?

为什么?

采纳答案by Berit Larsen

The solution was

解决办法是

Arrays.sort(FishNamesSorted, String.CASE_INSENSITIVE_ORDER)

I had misunderstood how it works

我误解了它的工作原理

回答by jmcg

Collections.sort(list)definitely works. The problem in your code is you placed the list into the array before sorting. The array should be sorted if you sort your list first before putting it to the array

Collections.sort(list)绝对有效。您代码中的问题是您在排序之前将列表放入数组中。如果在将列表放入数组之前先对列表进行排序,则应该对数组进行排序

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

/* add elements to namesList */

Collections.sort(nameslist);
Object[] fishNamesSorted = nameslist.toArray();

回答by Sufiyan Ghori

You are supposed to put your nameslistin your FishNamesSortedarray only after it is sorted, which you are not doing right now.

您应该仅在排序后将nameslist您的FishNamesSorted数组放入数组中,而您现在没有这样做。

have a look,

看一看,

    String[] FishNamesSorted;

    // creating and initializing list,
    List<String> nameslist = new ArrayList<String>();

    // Adding some data in your list
    nameslist.add("Poecilia Latipinna");
    nameslist.add("Poecilia Reticulata");
    nameslist.add("Notropis Chrosomus");
    nameslist.add("Pseudomugil Gertrudae");

    // sorting your list,
    Collections.sort(nameslist);

    // print sorted list
    for (String s : nameslist){
        System.out.println(s);
    }

    System.out.println("===================");

    // convert the sorted list to an array and assign it 
    // a String array.
    FishNamesSorted = nameslist.toArray((new String[nameslist.size()]));

    // print your String array,
    for (String s : FishNamesSorted){
        System.out.println(s);
    }

just FYI, you can make your sorting process work even faster if you are using Java 8.

仅供参考,如果您使用 Java 8,您可以使排序过程更快。

Java 8 provides an API for sorting any type of array using Arrays.parallelSort(type), it performs sorting the same way as Collection.sortbut with a parallel implementation.

Java 8 提供了一个 API 来使用 对任何类型的数组Arrays.parallelSort(type)进行排序,它执行排序的方式Collection.sort与并行实现相同。

Current sorting implementations provided by the Java Collections Framework > (Collections.sortand Arrays.sort) all perform the sorting operation sequentially in the calling thread. This enhancement will offer the same set of sorting operations currently provided by the Arrays class, but with a parallel implementation that utilizes the Fork/Join framework. These new API's are still synchronous with regard to the calling thread as it will not proceed past the sorting operation until the parallel sort is complete.

Java Collections Framework > (Collections.sortArrays.sort)提供的当前排序实现都在调用线程中按顺序执行排序操作。此增强功能将提供与 Arrays 类当前提供的相同的一组排序操作,但具有利用 Fork/Join 框架的并行实现。这些新 API 仍然与调用线程同步,因为在并行排序完成之前它不会继续进行排序操作。

to implement it, replace Collections.sortwith Arrays.parallelSortin the above code,

要实现它,取代Collections.sortArrays.parallelSort在上面的代码,

replace,

代替,

Collections.sort(nameslist);

with,

和,

Arrays.parallelSort(nameslist.toArray(new String[nameslist.size()]));