简体   繁体   English

Collections.sort没有排序任何东西

[英]Collections.sort is not sorting anything

I'm trying to sort a String array in a short, simple way. 我试图以一种简短的方式对String数组进行排序。 I'm trying to use Collections.sort, but I don't understand why it doesn't sort anything. 我正在尝试使用Collections.sort,但我不明白为什么它不对任何内容进行排序。 Code: 码:

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 Latipinna
  • Poecilia Reticulata 网纹Poecilia
  • Notropis Chrosomus 嗜盐菌
  • Pseudomugil Gertrudae 假木耳Ger
  • .... ....

Whyyyy? 为什么呢?

Collections.sort(list) definitely works. Collections.sort(list)绝对可以。 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 如果在将列表放入数组之前先对列表进行排序,则应该对数组进行排序

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

/* add elements to namesList */

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

You are supposed to put your nameslist in your FishNamesSorted array 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.sort but with a parallel implementation. Java 8提供了一个使用Arrays.parallelSort(type)对任何类型的数组进行排序的API,它执行与Collection.sort相同的排序方式,但是具有并行实现。

Current sorting implementations provided by the Java Collections Framework > ( Collections.sort and Arrays.sort ) all perform the sorting operation sequentially in the calling thread. Java Collections Framework>( Collections.sortArrays.sort )提供的当前排序实现都在调用线程中按顺序执行排序操作。 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. 此增强功能将提供与Arrays类当前提供的相同的排序操作集,但具有利用Fork / Join框架的并行实现。 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. 这些新的API在调用线程方面仍然是同步的,因为在并行排序完成之前,它不会继续通过排序操作。

to implement it, replace Collections.sort with Arrays.parallelSort in the above code, 要实现它, Arrays.parallelSort在上面的代码Arrays.parallelSort Collections.sort替换为Arrays.parallelSort

replace, 更换,

Collections.sort(nameslist);

with, 与,

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

The solution was 解决方案是

Arrays.sort(FishNamesSorted, String.CASE_INSENSITIVE_ORDER)

I had misunderstood how it works 我误会了它的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM