简体   繁体   English

对作为数字的字符串的ArrayList进行排序

[英]Sort an ArrayList of Strings that are numbers

What is the fastest way to sort an ArrayList<String> (in descending/ascending manner) that contains numbers, eg: { "12", "3.5", "188", "33.03" } ? 对包含数字的ArrayList<String> (以递减/递增方式)进行排序的最快方法是什么,例如: { "12", "3.5", "188", "33.03" } Does Collections have a built-in method for this? Collections是否有内置的方法? Currently I am copying the ArrayList 's contents to ArrayList<Double> and then using Collections.Sort() method and then putting it back to the initial array. 目前我正在将ArrayList的内容复制到ArrayList<Double> ,然后使用Collections.Sort()方法,然后将其放回到初始数组。 Is there a faster way? 有更快的方法吗?

If you are using Java 8, you can use Comparator.comparing(Double::parseDouble) to quickly create a comparator using parseDouble . 如果您使用的是Java 8,则可以使用Comparator.comparing(Double::parseDouble)使用parseDouble快速创建比较器。 This should (see below) call the function just once for each entry, and not once for each pair. 这应该(见下文)为每个条目调用一次函数,而不是每对一次。

List<String> list = Arrays.asList( "12", "3.5", "188", "33.03" );
list.sort(Comparator.comparing(Double::parseDouble));
System.out.println(list);

Output: 输出:

[3.5, 12, 33.03, 188]

Update: Well, I thought this would call the comparator function just once for each element, like using a key -function in Python, but after a quick test using a function increasing a counter each time it is called, the function is called just as often as using an old-style "pair"-comparator. 更新:嗯,我认为这会为每个元素调用比较器函数一次,比如在Python中使用key -function,但是在每次调用时使用函数增加计数器的快速测试后,函数被调用为通常使用旧式“配对”比较器。 Still, a bit shorter... 还是,有点短......

You need to implement your own comparator, and use it on your list. 您需要实现自己的比较器,并在列表中使用它。 You have to use BigDecimal, because you can have problems with loss of precision. 您必须使用BigDecimal,因为您可能会遇到精度损失的问题。 You can use double, if your numbers are quire small precision. 你可以使用double,如果你的数字要求小精度。

class MyComparator implements Comparator<String, String> {

    public int compare(String o1, String o2){
        return new BigDecimal(o1).compareTo(new BigDecimal(o2));
    }

}
...
Collections.sort(list, new MyComparator());

Try following code: 请尝试以下代码:

String test[] = {"12", "3.5", "188", "33.03"};
double numbers[] = new double[test.length];
for (int i = 0; i < test.length; i++) {
     numbers[i] = Double.parseDouble(test[i]);
}
Arrays.sort(numbers);
for (double i : numbers) {
     System.out.println(i);
}

Output : 输出:

3.5
12.0
33.03
188.0

我认为您当前的方法可能很好,我会避免使用自定义Comparator因为您最终会将相同的字符串多次转换为数值(每次排序算法要比较2个值)而不是像您一样现在。

You can use Collections.sort() on List<String> , String is Comparable , but that comparison won't give you the right result. 您可以在List<String>上使用Collections.sort() ,String是Comparable ,但该比较不会为您提供正确的结果。

Also you can define your own Comparator , and pass it to Collections.sort() . 您也可以定义自己的Comparator ,并将其传递给Collections.sort()

您可以尝试使用sortedset接口,它可以在输入数据时为您提供排序数据。更好地实现您自己的比较器,我相信这对我来说没什么用处。

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

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