How do I sort an array of objects with a Comparator?
Author: Deron Eriksson
Description: This Java tutorial describes how to sort an array of objects with a Comparator.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The AvailableLocalesUnsortedAndSorted class gives an example of how an array of objects can be sorted with a Comparator. Comparator is an interface that requires the implementation of the compare() method. The compare method lets us write sorting code to compare two objects and determine which object should go first. If we have the compare method return a negative int, the first argument is less than the second. If it returns 0, the objects are equal in terms of sort order. If it returns a positive in, the first argument is greater than the second.

The AvailableLocalesUnsortedAndSorted class gets an array of the available locales and displays them in the default order. It then creates a localComparator object that implements the Comparator interface to allow us to sort on the Locale objects' toString() values. The Arrays.sort(locales, localeComparator) call specifies to sort the array of Locale objects using the sort logic in localeComparator, which sorts according to the Local objects' toString() values. It then displays the available locales in their newly sorted order.

AvailableLocalesUnsortedAndSorted.java

package test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;

public class AvailableLocalesUnsortedAndSorted {

	public static void main(String[] args) throws Exception {

		Locale locales[] = Locale.getAvailableLocales();

		System.out.println("Unsorted:");
		displayLocales(locales);

		Comparator<Locale> localeComparator = new Comparator<Locale>() {
			public int compare(Locale locale1, Locale locale2) {
				return locale1.toString().compareTo(locale2.toString());
			}
		};
		Arrays.sort(locales, localeComparator);

		System.out.println("Sorted:");
		displayLocales(locales);
	}

	public static void displayLocales(Locale[] locales) {
		for (Locale locale : locales) {
			System.out.print(locale.toString());
			if (locale != locales[locales.length - 1]) {
				System.out.print(", ");
			}
		}
		System.out.println("\n");
	}
}

The output of AvailableLocalesUnsortedAndSorted is shown here:

Unsorted: ar, ar_AE, ar_BH, ar_DZ, ar_EG, ar_IQ, ar_JO, ar_KW, ar_LB, ar_LY, ar_MA, ar_OM, ar_QA, ar_SA, ar_SD, ar_SY, ar_TN, ar_YE, hi_IN, iw, iw_IL, ja, ja_JP, ko, ko_KR, th, th_TH, th_TH_TH, vi, vi_VN, zh, zh_CN, zh_HK, zh_TW, be, be_BY, bg, bg_BG, ca, ca_ES, cs, cs_CZ, da, da_DK, de, de_AT, de_CH, de_DE, de_LU, el, el_GR, en_AU, en_CA, en_GB, en_IE, en_IN, en_NZ, en_ZA, es, es_AR, es_BO, es_CL, es_CO, es_CR, es_DO, es_EC, es_ES, es_GT, es_HN, es_MX, es_NI, es_PA, es_PE, es_PR, es_PY, es_SV, es_UY, es_VE, et, et_EE, fi, fi_FI, fr, fr_BE, fr_CA, fr_CH, fr_FR, fr_LU, hr, hr_HR, hu, hu_HU, is, is_IS, it, it_CH, it_IT, lt, lt_LT, lv, lv_LV, mk, mk_MK, nl, nl_BE, nl_NL, no, no_NO, no_NO_NY, pl, pl_PL, pt, pt_BR, pt_PT, ro, ro_RO, ru, ru_RU, sk, sk_SK, sl, sl_SI, sq, sq_AL, sr, sr_BA, sr_CS, sv, sv_SE, tr, tr_TR, uk, uk_UA, en_US, en

Sorted: ar, ar_AE, ar_BH, ar_DZ, ar_EG, ar_IQ, ar_JO, ar_KW, ar_LB, ar_LY, ar_MA, ar_OM, ar_QA, ar_SA, ar_SD, ar_SY, ar_TN, ar_YE, be, be_BY, bg, bg_BG, ca, ca_ES, cs, cs_CZ, da, da_DK, de, de_AT, de_CH, de_DE, de_LU, el, el_GR, en, en_AU, en_CA, en_GB, en_IE, en_IN, en_NZ, en_US, en_ZA, es, es_AR, es_BO, es_CL, es_CO, es_CR, es_DO, es_EC, es_ES, es_GT, es_HN, es_MX, es_NI, es_PA, es_PE, es_PR, es_PY, es_SV, es_UY, es_VE, et, et_EE, fi, fi_FI, fr, fr_BE, fr_CA, fr_CH, fr_FR, fr_LU, hi_IN, hr, hr_HR, hu, hu_HU, is, is_IS, it, it_CH, it_IT, iw, iw_IL, ja, ja_JP, ko, ko_KR, lt, lt_LT, lv, lv_LV, mk, mk_MK, nl, nl_BE, nl_NL, no, no_NO, no_NO_NY, pl, pl_PL, pt, pt_BR, pt_PT, ro, ro_RO, ru, ru_RU, sk, sk_SK, sl, sl_SI, sq, sq_AL, sr, sr_BA, sr_CS, sv, sv_SE, th, th_TH, th_TH_TH, tr, tr_TR, uk, uk_UA, vi, vi_VN, zh, zh_CN, zh_HK, zh_TW