What is the fastest way to sort an ArrayList<String> (in descending/ascending manner) that contains numbers, eg: { "12", "3.5", "188", "33.03" } ? Does Collections have a built-in method for this? 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. Is there a faster way?
- 
        You could implement your own Comparator, not sure if faster though.sp00m– sp00m2014-10-13 11:54:24 +00:00Commented Oct 13, 2014 at 11:54
 - 
        5why do you want to insert numbers as string in an arraylistGeorge Thomas– George Thomas2014-10-13 11:54:37 +00:00Commented Oct 13, 2014 at 11:54
 - 
        @GeorgeThomas Long story short: maintaining somebody else's code.astralmaster– astralmaster2014-10-13 11:55:04 +00:00Commented Oct 13, 2014 at 11:55
 - 
        2@GeorgeThomas I agree, if it were me, I would make them all doublesDreadHeadedDeveloper– DreadHeadedDeveloper2014-10-13 11:55:06 +00:00Commented Oct 13, 2014 at 11:55
 - 
        Check this answer [here][1]. It works for every type of variables. [1]: stackoverflow.com/a/12771571/4096270Pedro Loureiro– Pedro Loureiro2014-10-13 12:01:59 +00:00Commented Oct 13, 2014 at 12:01
 
6 Answers
If you are using Java 8, you can use Comparator.comparing(Double::parseDouble) to quickly create a comparator using 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. Still, a bit shorter...
3 Comments
List has a sort method.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. You can use double, if your numbers are quire small precision.
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());
    1 Comment
Double.parseDouble() instead BigDecimal if you know the values are of limited precision.I think your current approach is probably fine, I would avoid using a custom Comparator because you would end up converting the same string into a numeric value multiple times (each time the sorting algorithm wants to compare 2 values) rather than just once as you do now.
2 Comments
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
    7 Comments
Floats here due to the possible loss of accuracy (depending of course on the input values) but in principle this should be fine.