Steve
Steve

Reputation: 6444

Why can't I call Collections.sort() on my ArrayList<T>?

For anyone who might have a question like this, you probably need "Collections.sort", not "Collection.sort", the mistake I made below.

I have defined a class defined as

public class Store implements Serializable, Comparable<Store> { ... }

I have a field in another class defined as:

ArrayList<Store> fStores = new ArrayList<Store>();

I want to sort this collection, so in a method I call:

Collection.sort(fStores);

However, I get the following compilation error:

The method sort(ArrayList<Store>) is undefined for the type Collection

ArrayList implements List, and from the documentation:

public static <T extends Comparable<? super T>> void sort(List<T> list)

So, why do I get the error? I have also tried creating my own descendant of Comparator and passing that to the sort method with no luck.

I'm guessing there's something about "< T extends Comparable< ? super T > >" I'm not understanding... ?

Upvotes: 7

Views: 30807

Answers (8)

Noam Manos
Noam Manos

Reputation: 16990

In case you got here due to a similar error:

The method sort(Comparator<...>) is undefined for the type List<...>

Then you're probably using JDK lower then 1.8. Change it in Java Build Path:

enter image description here

Upvotes: 0

user4373561
user4373561

Reputation: 1

Also, if you are using eclipse, double check that the import statement for Collections is the right one!

Upvotes: 0

S.P.
S.P.

Reputation: 3054

There are basically 2 things that you need to look at :

Collections

From the Collections

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends

So basically if you have to sort or do any such kind of algorithms use this.

Next is :->

Collection

This is an interface that provides the basis of Java's collection framework. It does not include Map and Sorted Map. It represents a group of objects known as its elements and has implementations for concrete implementations. You need to think of this when you want to work with ArrayLists and Maps.

So, bottom line, you have a static algorithm to run which is present in Collections. So, use Collections.sort

Upvotes: 7

Ryan Amos
Ryan Amos

Reputation: 5452

Your issue is that you are calling Collection.sort(), not Collections.sort()

These are two separate classes:

Collections

Collection

Collection is an interface while Collections is a utility class.

Upvotes: 1

Thorn G
Thorn G

Reputation: 12766

It's Collections.sort() with an s.

import java.util.Collections and you should be fine.

Upvotes: 1

jacobm
jacobm

Reputation: 14035

Did you mean Collections.sort() (collections plural)?

Upvotes: 1

ruakh
ruakh

Reputation: 183446

You need to write Collections instead of Collection. They're related, but different. :-)

Upvotes: 4

Related Questions