How to Use Collections.Sort() in Java

How to use Collections.sort() in Java?

Use this method Collections.sort(List,Comparator) . Implement a Comparator and pass it to Collections.sort().

class RecipeCompare implements Comparator<Recipe> {

@Override
public int compare(Recipe o1, Recipe o2) {
// write comparison logic here like below , it's just a sample
return o1.getID().compareTo(o2.getID());
}
}

Then use the Comparator as

Collections.sort(recipes,new RecipeCompare());

How to sort a CollectionT?

Collections by themselves do not have a predefined order, therefore you must convert them to
a java.util.List. Then you can use one form of java.util.Collections.sort

Collection< T > collection = ...;

List< T > list = new ArrayList< T >( collection );

Collections.sort( list );
// or
Collections.sort( list, new Comparator< T >( ){...} );

// list now is sorted

Sorting a collection of objects

Implement the Comparator interface (once for each different sort order) and use the Collections.sort() method that takes a Comparator as additional parameter.

Collections sort(ListT,Comparator? super T) method example

Building upon your existing Student class, this is how I usually do it, especially if I need more than one comparator.

public class Student implements Comparable<Student> {

String name;
int age;

public Student(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public String toString() {
return name + ":" + age;
}

@Override
public int compareTo(Student o) {
return Comparators.NAME.compare(this, o);
}

public static class Comparators {

public static Comparator<Student> NAME = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
};
public static Comparator<Student> AGE = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.age - o2.age;
}
};
public static Comparator<Student> NAMEANDAGE = new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int i = o1.name.compareTo(o2.name);
if (i == 0) {
i = o1.age - o2.age;
}
return i;
}
};
}
}

Usage:

List<Student> studentList = new LinkedList<>();
Collections.sort(studentList, Student.Comparators.AGE);

EDIT

Since the release of Java 8 the inner class Comparators may be greatly simplified using lambdas. Java 8 also introduces a new method for the Comparator object thenComparing, which removes the need for doing manual checking of each comparator when nesting them. Below is the Java 8 implementation of the Student.Comparators class with these changes taken into account.

public static class Comparators {
public static final Comparator<Student> NAME = (Student o1, Student o2) -> o1.name.compareTo(o2.name);
public static final Comparator<Student> AGE = (Student o1, Student o2) -> Integer.compare(o1.age, o2.age);
public static final Comparator<Student> NAMEANDAGE = (Student o1, Student o2) -> NAME.thenComparing(AGE).compare(o1, o2);
}

Using Collections.sort() method to sort objects alphabetically

You got the error because when you call Collections.sort() passing only a List<T> as parameter, it expects that the list elements implement the Comparable interface. Since this is not the case for CreateShape, sort() has no way to know how these objects should be sorted.

Here are two options you should consider:

  1. CreateShape could implement Comparable<CreateShape>: do this if you think CreateShape instances have a natural order in which they should be sorted. If you wanted to sort by a char field, for example:

    class CreateShape implements Comparable<CreateShape> {
    private char displayChar;

    public char getDisplayChar() {
    return displayChar;
    }

    @Override
    public int compareTo(CreateShape that) {
    return Character.compare(this.displayChar, that.displayChar);
    }
    }

Then you could simply call Collections.sort():

Collections.sort(shapes);

  1. Create a custom Comparator<CreateShape>: do this if you want to sort CreateShape instances arbitrarily. You could have a Comparator that sorts by name, another that sorts by id, etc. Example:

    enum DisplayCharComparator implements Comparator<CreateShape> {
    INSTANCE;

    @Override
    public int compare(CreateShape s1, CreateShape s2) {
    return Character.compare(s1.getDisplayChar(), s2.getDisplayChar());
    }
    }

Then you should call Collections.sort() passing the comparator as parameter:

Collections.sort(shapes, DisplayCharComparator.INSTANCE);

Note I implemented DisplayCharComparator as a singleton. That's because it has no state, so there is no need to have more than one instance of this comparator. An alternative is to use a static variable:

class CreateShape {

static final Comparator<CreateShape> DISPLAY_CHAR_COMPARATOR =
new DisplayCharComparator();

static class DisplayCharComparator implements Comparator<CreateShape> { ... }

// rest of the code
}

Or, if you're using Java 8, you can use Comparator.comparing:

shapes.sort(Comparator.comparing(CreateShape::getDisplayChar));

Sorting of Strings using collections.sort() method

The "natural ordering" that Collections.sort refers to is the one specified by Comparable -- which String implements, and which defines only one method, compareTo. So, the answer is in the definition of String.compareTo. Its documentation states:

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

Lexicographical ordering basically means dictionary ordering. Essentially, order each letter alphabetically as far as you go, but if you're still tied when either word runs out of letters, then the shorter word goes first.

Unicode is the numerical value that each character has. There's a great introductory post about it here (it's not short, but it does a good job of walking you through not just what unicode is, but why it exists).

Difference between Collections.sort(list) and Collections.sort(list,comparator)

Collections.sort(List<T>) sorts the given List by the natural ordering of its elements. The natural ordering of an object can be defined by implementing the Comparable interface in the corresponding class. This interface provides a single method, compareTo, which returns

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

On the other hand, Collections.sort(List<T>, Comparator<T>) orders the List's elements according to the given Comparator. Their natural ordering will be ignored for the sorting. This second method comes in hand when the List's elements already possess their natural ordering but we want to order them by a different criteria.

Here's a simple example with a Person class displaying name, last name and age.

class Person implements Comparable<Person> {
private String name, lastName;
private int age;

public Person(String name, String lastName, int age) {
this.name = name;
this.lastName = lastName;
this.age = age;
}

public String getName() {
return name;
}

public String getLastName() {
return lastName;
}

public int getAge() {
return age;
}

@Override
public int compareTo(Person o) {
//Defining Person's natural ordering by creating a comparator and comparing by last name and then name the current object and the given paramter
return Comparator.comparing(Person::getLastName).thenComparing(Person::getName).compare(this, o);
}

@Override
public String toString() {
return String.format("%s %s %d", name, lastName, age);
}

public static void main(String[] args) {
List<Person> list = new ArrayList<>(List.of(
new Person("Matt", "O'Brien", 30),
new Person("Conan", "O'Brien", 25),
new Person("Frank", "Johnson", 50)
));

//Original unordered list
System.out.println(list);

//List ordered by Person's natural ordering (last name and then name)
Collections.sort(list);
System.out.println(list);

//List ordered by custom criteria (age)
Collections.sort(list, Comparator.comparing(Person::getAge));
System.out.println(list);
}
}

Collections.sort sorts the wrong list

On this line:

ArrayList<Integer> returnList = list;

you are just creating another reference to the same list (Object) in your sorted method and any change that you apply to it using this new reference will be reflected in you original reference because they point to the same object. You can do this to create a new list:

private static ArrayList<Integer> sorted(ArrayList<Integer> list){
ArrayList<Integer> returnList = new ArrayList<>(list); // the new keyword creates a new object on the memory heap
Collections.sort(returnList);
return returnList;
}

This time we are creating another ArrayList Object with the elements of you original list. This way the original list won't change when you sort the newer.

This behaviour doesn't apply on Immutable Objects like String or LocalDateTime. These cannot change their state after being created and instead return a new copy with the changes applied.



Related Topics



Leave a reply



Submit