Java has a defined Collections framework, built for accessing, storing and manipulating Lists. Some classes that belong to this framework include: ArrayList
It belongs to the java.util
Java Package.
.sort()
Collections.sort()
is a Static method that sorts a List, provided all elements in the list extend the Generic Comparable<T>
Interface.
public interface Comparable<T> {
int compareTo(T other);
}
public class Circle implements Comparable<T> {
//Assume a compareTo method exists
//Assume a constructor based on radius exists
}
---
import java.util.ArrayList;
import java.util.Collections;
ArrayList<Circle> circleList = new ArrayList<>();
ArrayList.add(new Circle(3));
ArrayList.add(new Circle(7));
Collections.sort(circleList);