-2

If I want to sort the Collection below by a property called Order on the CSVInputHandler class, how do I do that? I tried the one a the very bottom with no luck. The error says The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Collection<CSVInputHandler>, Comparator<CSVInputHandler>).

Object

Collection<CSVInputHandler> csvInputHandlers = new ArrayList<>(csvInputHandlerMap.values());

Tried

  Comparator<CSVInputHandler> comparator = new Comparator<CSVInputHandler>() {
                    public int compare(CSVInputHandler c1, CSVInputHandler c2) {
                        return c1.Order < c2.Order;
                    }
                };

                Collections.sort(csvInputHandlers, comparator); 
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
  • 1
    What is the type of `Order`? You're returning 0 from your `compare()` method which is pointless – Kon Jan 21 '15 at 21:27
  • 2
    Collections.sort requires a List not an array. I suggest you to read the docs and read the compile errors you are getting. – user2336315 Jan 21 '15 at 21:28
  • I am just using 0 as a test to get the syntax error to go away. – Mike Flynn Jan 21 '15 at 21:28
  • I know that, but how do you SORT a collection. – Mike Flynn Jan 21 '15 at 21:28
  • 1
    possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – user2336315 Jan 21 '15 at 21:29
  • You get rid of `.toArray()`, since that is changing your List into an array. – Teepeemm Jan 21 '15 at 21:30
  • Not a duplicate, I want to sort a COLLECTION! – Mike Flynn Jan 21 '15 at 21:30
  • Removing .toArray() still causes an error. – Mike Flynn Jan 21 '15 at 21:30
  • @MikeFlynn not by using Collections... You have to convert your Collection to a List. – gtgaxiola Jan 21 '15 at 21:30
  • Ok how do I do that???? – Mike Flynn Jan 21 '15 at 21:31
  • @MikeFlynn Come on. Does it make sense to you to sort a Collection? An HashSet is a Collection. Does it make sense to you to sort an HashSet? – user2336315 Jan 21 '15 at 21:31
  • I dont care how I can get it sorted, convert to something else, I just want an answer where I can sort it and iterate over the sorted values. Lets just say I cant change the object type of Collection, answer the question. – Mike Flynn Jan 21 '15 at 21:32
  • Collection are not ordered in Java because they contains Set for example, so there is no way to order a collection ;) – Gab Jan 21 '15 at 21:32
  • `Collections.sort(new ArrayList(csvInputHandlers ), comparator)` – gtgaxiola Jan 21 '15 at 21:32
  • Just do `List csvInputHandlers = new ArrayList<>(csvInputHandlerMap.values());` instead. – user2336315 Jan 21 '15 at 21:35
  • @MikeFlynn Just to tell you. Go read the doc first, check the method signature, try to understand the compile error you are getting. When you are doing Collection csvInputHandlers, the type is Collection even if the real reference type behind is List. This is really a fundamental concept if you got it then you're fine. – user2336315 Jan 21 '15 at 21:37
  • Is Order object an ENUM? – MaxZoom Jan 21 '15 at 21:40
  • @user2336315 I know that, reading the docs wont tell me the correct way to get a collection to a list. Read the answer below. – Mike Flynn Jan 21 '15 at 21:42
  • @MikeFlynn The doc clearly states that sort takes a LIST as parameter and you are giving it a COLLECTION (even if the real implementation behind is an ArrayList). So yes reading the docs would have give you the answer. And there are already a dozen of answers on google. Anyway if you don't get it that's not my problem. – user2336315 Jan 22 '15 at 05:17

4 Answers4

2

In order to sort a Collection (by using Collections.sort) you must explicitly convert it into a List

List<CSVInputHandler> myList = new ArrayList<>(csvInputHandlers);

Collections.sort(myList, comparator);
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
0

Remove .toArray() as that sort method accept only List types not Array

Collections.sort(csvInputHandlers, comparator); 

Def

public static <T> void sort(List<T> list, Comparator<? super T> c) {
terma
  • 1,199
  • 1
  • 8
  • 15
0

The Collections#sort method acts on Collections so the first method argument must be a Collection (i.e. any concrete subtype such as List, Set) while you are trying to as an array element as a first argument. It should be

Collections.sort(csvInputHandlers, comparator)
tmarwen
  • 15,750
  • 5
  • 43
  • 62
0

Collection :

The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered

source http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html

Gab
  • 7,869
  • 4
  • 37
  • 68