Collections.sort 没有对任何东西进行排序
Collections.sort is not sorting anything
我正在尝试以一种简短的方式对字符串数组进行排序。
我正在尝试使用 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);
}
});
两种情况下的结果:
- 拉丁文
- 网纹刺槐
- Chrosomus
- 假单胞菌科
- .....
为什么yyy?
Collections.sort(list)
绝对有效。您的代码中的问题是您在排序之前将列表放入数组。如果在将列表放入数组之前先对列表进行排序,则应该对数组进行排序
List<String> nameslist = new ArrayList<String>();
/* add elements to namesList */
Collections.sort(nameslist);
Object[] fishNamesSorted = nameslist.toArray();
您应该仅在排序后才将 nameslist
放入 FishNamesSorted
数组,而您现在没有这样做。
看看,
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);
}
仅供参考,如果您使用 Java 8.
,您可以使排序过程更快
Java 8 提供了一个 API 用于使用 Arrays.parallelSort(type)
对任何类型的数组进行排序,它执行排序的方式与 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. 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.
实现,将上面代码中的Collections.sort
替换为Arrays.parallelSort
,
替换,
Collections.sort(nameslist);
与
Arrays.parallelSort(nameslist.toArray(new String[nameslist.size()]));
解决方案是
Arrays.sort(FishNamesSorted, String.CASE_INSENSITIVE_ORDER)
我误解了它的工作原理
我正在尝试以一种简短的方式对字符串数组进行排序。 我正在尝试使用 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);
}
});
两种情况下的结果:
- 拉丁文
- 网纹刺槐
- Chrosomus
- 假单胞菌科
- .....
为什么yyy?
Collections.sort(list)
绝对有效。您的代码中的问题是您在排序之前将列表放入数组。如果在将列表放入数组之前先对列表进行排序,则应该对数组进行排序
List<String> nameslist = new ArrayList<String>();
/* add elements to namesList */
Collections.sort(nameslist);
Object[] fishNamesSorted = nameslist.toArray();
您应该仅在排序后才将 nameslist
放入 FishNamesSorted
数组,而您现在没有这样做。
看看,
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);
}
仅供参考,如果您使用 Java 8.
,您可以使排序过程更快Java 8 提供了一个 API 用于使用 Arrays.parallelSort(type)
对任何类型的数组进行排序,它执行排序的方式与 Collection.sort
相同,但具有并行实现。
Current sorting implementations provided by the Java Collections Framework > (
Collections.sort
andArrays.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.
实现,将上面代码中的Collections.sort
替换为Arrays.parallelSort
,
替换,
Collections.sort(nameslist);
与
Arrays.parallelSort(nameslist.toArray(new String[nameslist.size()]));
解决方案是
Arrays.sort(FishNamesSorted, String.CASE_INSENSITIVE_ORDER)
我误解了它的工作原理