0

I know how to sort any kind of object using Comparator and Collections.sort() but I want to know how can I use Arrays.parallelSort() to sort the list of Maps ? since it is capable of sorting only normal arrays.

This is my code to sort it using Comparator,

List<Map<String, Integer>> employees = new ArrayList<Map<String, Integer>>() {{
    add(new HashMap<String, Integer>() {{put("position",5); put("id2", 9);}});
    add(new HashMap<String, Integer>() {{put("position",1); put("id2", 1);}});
    add(new HashMap<String, Integer>() {{put("position",2); put("id2", 2);}});
    add(new HashMap<String, Integer>() {{put("position",4); put("id2", 5);}});
    add(new HashMap<String, Integer>() {{put("position",1); put("id2", 1);}});
    add(new HashMap<String, Integer>() {{put("position",4); put("id2", 7);}});
}};

Comparator<Map<String, Integer>> comparator = new Comparator<Map<String, Integer>>() {
        @Override
        public int compare(Map<String, Integer> o1,Map<String, Integer> o2) {
                int nr1 = o1.get("id2");
                int nr2 = o2.get("id2");
                return Integer.compare(nr2, nr1);
        }
};

Collections.sort(employees,comparator);

for (Map<String, Integer> s : employees){
    System.out.println(s);
}

Arrays.parallelSort does have a method called parallelSort(T[] a,Comparator<?super T> c) but I don't know how to use it properly.

I have tried this so far,

Arrays.parallelSort(new ArrayList<Map<String, Integer>>(employees.size()), comparator);

Ofcourse I would get this error,

The method parallelSort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ArrayList<Map<String,Integer>>, Comparator<Map<String,Integer>>)

I am just curious if such type of data can be sorted using parallelSort ?

P.S: I also know how to use Java 8 stream().sorted for sorting but I don't want to use it.

Edit: I am sorting id2 in descending order.

bhuvesh
  • 729
  • 1
  • 9
  • 21
  • I think this post can help you : [here](http://stackoverflow.com/questions/25961018/sorting-a-list-in-parallel-without-creating-a-temporary-array-in-java-8) – Thomas H Jan 28 '15 at 19:17

1 Answers1

2

For the specific example that you show in your question, you could simply write:

employees.parallelStream()
        .sorted((m1, m2) -> Integer.compare(m2.get("id2"), m1.get("id2")))
        .forEachOrdered(System.out::println);

But note that it will probably call Arrays#parallelSort in the background.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 2
    `Comparator.comparing(m -> m.get("id2")).reversed()` – aioobe Jan 28 '15 at 21:25
  • why write it `Arrays#parallelSort` like this, and not like this `Arrays.parallelSort` , when discussing methods ? – bhuvesh Jan 29 '15 at 11:06
  • @assylias I tried your code but it always gives so many errors, `m1 cannot be resolved to a variable` ... `m2 cannot be resolved to a variable` and on `System.out::println` it gives `Syntax error on tokens, delete these tokens` – bhuvesh Jan 29 '15 at 11:18
  • 1
    `#` is used in javadoc. The error you get probably means that you are compiling with Java 7 or earlier instead of Java 8 - the code in my answer compiles fine with Java 8. I suggest you ask a separate question if you can't make it work. – assylias Jan 29 '15 at 11:36
  • thank you very much, I had to install Eclipse Juno for Java 1.8 compatibility. Now it is working fine. – bhuvesh Jan 29 '15 at 12:39