Java collection sorting

Hi

In this example I’ll be sorting out a list by using comparator and collator interfaces. Both of those allow us to sort a given collection list by a key we provide. Moreover collator will allow to sort a list by a localization if you are running a multiple language application, this will help you out a big time.

Before we begin keep in mind that I’ll use the array list implementation of the List interface in java. So first I’ll have a dummy list of countries in a few of languages Turkish, German and English to show the implementations in a multi language showing purpose.

Code Snippet

Country Pojo

package com.tugrulaslan.domain;

public class Country {

	public Country() {
		super();
	}

	public Country(String countryName) {
		super();
		this.countryName = countryName;
	}

	private String countryName;

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	@Override
	public String toString() {
		return "Country [countryName=" + countryName + "]";
	}

}

MainApp

import java.util.ArrayList;
import java.util.List;

import com.tugrulaslan.domain.Country;

public class MainApp {
	private static List<Country> countries = new ArrayList<Country>();

	static {
		Country country = new Country("Deutschland");
		Country country1 = new Country("Österreich");
		Country country2 = new Country("Türkei");
		Country country3 = new Country("Schweiz");
		Country country4 = new Country("Rumänien");
		Country country5 = new Country("Çek Cumhuriyeti");
		Country country6 = new Country("İngiltere");
		Country country7 = new Country("Vietnam");
		Country country8 = new Country("Australia");
		Country country9 = new Country("Belarus");

		countries.add(country);
		countries.add(country1);
		countries.add(country2);
		countries.add(country3);
		countries.add(country4);
		countries.add(country5);
		countries.add(country6);
		countries.add(country7);
		countries.add(country8);
		countries.add(country9);
	}

	public static void main(String[] args) {
                sortCollection();
		for (Country obj : countries)
			System.out.println(obj.getCountryName());
	}
	
	private static void sortCollection(){
		
	}

}

1. Natural sort/insertion

Java will insert all the elements in the insertion order if you are using ArrayList implementation

ref: http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#add-E-

when you launch the application without any sorting implementation the list will be printed like this:

Deutschland
Österreich
Türkei
Schweiz
Rumänien
Çek Cumhuriyeti
İngiltere
Vietnam
Australia
Belarus

2. Sorting using Comparator

Comparator is an interface which takes in an object whose field will be compared to ascending or descending.

ref: https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

This interface only brings the compareTo method which compares your object.

There are two ways for the implementation

1. Pojo class implements  the interface

package com.tugrulaslan.domain;

public class Country implements Comparable<Country>{

	public Country() {
		super();
	}

	public Country(String countryName) {
		super();
		this.countryName = countryName;
	}

	private String countryName;

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

	@Override
	public String toString() {
		return "Country [countryName=" + countryName + "]";
	}

	@Override
	public int compareTo(Country o) {
		return (this.countryName).compareTo(o.countryName);
	}

}

2. Separate interface implementation class 

CountryComparator

package com.tugrulaslan.comparator;

import java.util.Comparator;

import com.tugrulaslan.domain.Country;

public class CountryComparator implements Comparator<Country> {

	@Override
	public int compare(Country o1, Country o2) {
		return o1.getCountryName().compareTo(o2.getCountryName());
	}

}

if you compare 01, to 02 the list will be sorted descending, the opposite will result the list to be sorted ascending

descending sorted output

Australia
Belarus
Deutschland
Rumänien
Schweiz
Türkei
Vietnam
Çek Cumhuriyeti
Österreich
İngiltere

ascending sorted output

İngiltere
Österreich
Çek Cumhuriyeti
Vietnam
Türkei
Schweiz
Rumänien
Deutschland
Belarus
Australia

Updated method

private static void sortCollection(){
		Collections.sort(countries, new CountryComparator());
	}

3. Collator locale specific sorting

As I previously mentioned, java allows you to sort lists in your locale. There are two ways to implement it

Collator collator = Collator.getInstance(Locale.GERMAN);

and

Collator.getInstance(new Locale("tr", "TR"));

refs

https://docs.oracle.com/javase/7/docs/api/java/text/Collator.html

https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

https://docs.oracle.com/javase/tutorial/i18n/locale/create.html

1.Sort by the German locale

Australia
Belarus
Çek Cumhuriyeti
Deutschland
İngiltere
Österreich
Rumänien
Schweiz
Türkei
Vietnam

updated CountryComparator class

@Override
	public int compare(Country o1, Country o2) {
		Collator collator = Collator.getInstance(Locale.GERMAN);
		return collator.compare(o1.getCountryName(), o2.getCountryName());
	}

 

2.Sort by the Turkish locale

Australia
Belarus
Çek Cumhuriyeti
Deutschland
İngiltere
Österreich
Rumänien
Schweiz
Türkei
Vietnam

updated CountryComparator class

@Override
	public int compare(Country o1, Country o2) {
		Collator collator = Collator.getInstance(new Locale("tr", "TR"));
		return collator.compare(o1.getCountryName(), o2.getCountryName());
	}

Leave a Reply

Your email address will not be published. Required fields are marked *