简体   繁体   中英

Collections.sort is not sorting anything

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:

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?

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

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.

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 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.

Current sorting implementations provided by the Java Collections Framework > ( Collections.sort and 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.

to implement it, replace Collections.sort with Arrays.parallelSort in the above code,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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