3

I'm trying to order elements using native sort method.

Code:

List<String> list = new ArrayList();
Collections.sort(list);

Input 1:

Before order: 65 31 37 37 72 76 61 35 57 37
After order:  31 35 37 37 37 57 61 65 72 76
Expected:     Ok.

Input 2:

Before order: 45 186 185 55 51 51 22 78 64 26 49 21
After order:  185 186 21 22 26 45 49 51 51 55 64 78
Expected:     21 22 26 45 49 51 51 55 64 78 185 186

The problem is that the method is sorting wrong in some cases, how can I solve it?

2
  • 2
    use a Comparator Commented Mar 22, 2016 at 4:06
  • 1
    Can you tell us your expected result and the actual result ? Commented Mar 22, 2016 at 4:09

3 Answers 3

5

You have a List<String>, so Collections.sort is ordering the String(s) lexicographically. You could use a List<Integer> like

List<Integer> al = Arrays.asList(45, 186, 185, 55, 51, 51, 22, 78, 64, 26, 49, 21);
Collections.sort(al);
System.out.println(al);

But, if you must use String(s) then you'll need to provide a custom Comparator (because the default String ordering isn't what you want). Something like,

List<String> al = Arrays.asList("45", "186", "185", "55", "51", "51", //
        "22", "78", "64", "26", "49", "21");
Collections.sort(al, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2));
    }
});
System.out.println(al);

which outputs (like the first example)

[21, 22, 26, 45, 49, 51, 51, 55, 64, 78, 185, 186]
Sign up to request clarification or add additional context in comments.

2 Comments

It's probably worth pointing out that in Java 8 you can do it particularly succinctly: list.sort(Comparator.comparingInt(Integer::parseInt));
Thanks. And, yes I have to use it as String. Nice contribution @PaulBoddington. By the way, when I wrote that code of @ElliottFricsh, Netbeans shows me another option to use lambda: (String o1, String o2) -> Integer.compare(Integer.parseInt(o1), Integer.parseInt(o2)));. Just to contribute also.
0

You should be using Integer comparator.

List<Integer> list = new ArrayList();
Collections.sort(list);

or

List<String> list = Arrays.asList(new String[]{"45", "186", "185", "55", "51", "51", "22", "78", "64", "26", "49", "21"});
Comparator<String> cmp = (String o1, String o2) -> Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
Collections.sort(list,cmp);
System.out.println(list);

Comments

0

You should sort by

  1. integer value of string

That is

Collections.sort(list,
    Comparator.comparing(Integer::parseInt));

Or sort by

  1. length of string
  2. string itself

That is

Collections.sort(list,
    Comparator.comparing(String::length)
              .thenComparing(Function.identity()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.