Java List Sort sortLocales(final List locales)

Here you can find the source of sortLocales(final List locales)

Description

Sort the Locales alphabetically.

License

Open Source License

Parameter

Parameter Description
locales a parameter

Declaration

public static final void sortLocales(final List<Locale> locales) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Martin Reiterer./*w w  w .j  av  a  2s  . c o m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * @author Martin Reiterer
 * @author Christian Behon
 * @author Pascal Essiembre
 *
 ******************************************************************************/

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class Main {
    private static final Comparator<Locale> LOCALE_COMPERATOR = new Comparator<Locale>() {

        @Override
        public int compare(final Locale localeOne, final Locale localeTwo) {
            return getDisplayName(localeOne).compareToIgnoreCase(getDisplayName(localeTwo));
        }

        private String getDisplayName(final Locale locale) {
            String displayName;
            if (locale == null) {
                displayName = "Default";
            } else {
                displayName = locale.getDisplayName();
            }
            return displayName;
        }
    };

    /**
     * Sort the Locales alphabetically.
     * Make sure the root Locale is first.
     *
     * @param locales
     */
    public static final void sortLocales(final List<Locale> locales) {
        Collections.sort(locales, LOCALE_COMPERATOR);
    }
}

Related

  1. sortListAlphabetically(List list)
  2. sortListByMapKey(List> list)
  3. sortListEntry(List firstList, List secondList)
  4. sortListOfDoublesAndRemoveDuplicates(List items, double tolerance, double toleranceSmallNumbers)
  5. sortListStingArrayByFirstDes( List list)
  6. sortLongSeqList(List firstList, List secondList)
  7. sortLoops(ArrayList list)
  8. sortMap(Map targetMap, List sortedList)
  9. sortMapEntryListByValue(List> entries, boolean descending)