Example of Collections.sort in Java

By Arvind Rai, May 18, 2013
Java provides Collections.sort() that can sort List elements. Sorting is achieved by Comparable or Comparator. When using Comparable, all the elements of the List must implement Comparable interface. When using Comparator, we need to create a class implementing Comparator and the instance will be passed in Collections.sort() as an argument.

Collections.sort() with Comparable

sort() method sorts a given List into ascending order. The elements of the list must be comparable. According to java doc, the method syntax of sort() with Comparable is given below.
public static <T extends Comparable<? super T>> void sort(List<T> list) 
 
Now find the example
SortWithComparable.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortWithComparable {
    public static void main(String[] args) {
        Person a1 = new Person("Ram");
        Person a2 = new Person("Shyam");
        Person a3 = new Person("Rahim");
        List<Person> list = new ArrayList<Person>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        Collections.sort(list);
        for (Person a : list) {
            System.out.println(a.getName());
        }
    }
}
class Person implements Comparable<Person> {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    @Override
    public int compareTo(Person o) {
        return name.compareTo(o.name);
    }
    public String getName() {
	return name;
    }
} 
Output
Rahim
Ram
Shyam 

Collections.sort() with Comparator

sort() can also sort a List on the basis of the given Comparator. According to java doc, the method syntax of sort() with Comparator is given below.
public static <T> void sort(List<T> list, Comparator<? super T> c) 
 
Now find the example
SortWithComparator.java
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortWithComparator {
    public static void main(String[] args) {
        User a1 = new User("Ram");
        User a2 = new User("Shyam");
        User a3 = new User("Rahim");
        List<User> list = new ArrayList<User>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        Collections.sort(list, new UserComparator());
        for (User a : list) {
            System.out.println(a.getName());
        }
    }
}
class User {
    private String name;
    public User(String name) {
        this.name = name;
    }
    public String getName() {
	return name;
    }
} 
UserComparator.java
package com.concretepage.util;
import java.util.Comparator;
public class UserComparator implements Comparator<User> {
	@Override
	public int compare(User o1, User o2) {
		return o1.getName().compareTo(o2.getName());
	}
}  
Output
Rahim
Ram
Shyam 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us