How to reorder list <String>

I created the following method:

public List<String> listAll() { List worldCountriesByLocal = new ArrayList(); for (Locale locale : Locale.getAvailableLocales()) { final String isoCountry = locale.getDisplayCountry(); if (isoCountry.length() > 0) { worldCountriesByLocal.add(isoCountry); Collections.sort(worldCountriesByLocal); } } return worldCountriesByLocal; } 

Its quite simple and it returns a list of countries in the user's locale. Then I sort it to get it in alphabetical order. All this works great (except that I sometimes get duplicate countries!).

In any case, I need to place the USA and the UK at the top of the list. The problem is that I cannot isolate the index or row that will be returned for the USA and the UK, because it is typical for the locale!

Any ideas would be really appreciated.

0
java list locale
source share
4 answers

You can also use TreeSet to eliminate duplicates and your own Comparator to bring the US and GB to the beginning.

You get duplicates (which this will eliminate), because often there are several locales for each country. There is the USA (Spanish), as well as the USA (English), and there are, for example, three Switzerland (French, German and Italian).

 public class AllLocales { // Which Locales get priority. private static final Locale[] priorityLocales = { Locale.US, Locale.UK }; private static class MyLocale implements Comparable<MyLocale> { // My Locale. private final Locale me; public MyLocale(Locale me) { this.me = me; } // Convenience public String getCountry() { return me.getCountry(); } @Override public int compareTo(MyLocale it) { // No duplicates in the country field. if (getCountry().equals(it.getCountry())) { return 0; } // Check for priority ones. for (int i = 0; i < priorityLocales.length; i++) { Locale priority = priorityLocales[i]; // I am a priority one. if (getCountry().equals(priority.getCountry())) { // I come first. return -1; } // It is a priority one. if (it.getCountry().equals(priority.getCountry())) { // It comes first. return 1; } } // Default to straight comparison. return getCountry().compareTo(it.getCountry()); } } public static List<String> listAll() { Set<MyLocale> byLocale = new TreeSet(); // Gather them all up. for (Locale locale : Locale.getAvailableLocales()) { final String isoCountry = locale.getDisplayCountry(); if (isoCountry.length() > 0) { //System.out.println(locale.getCountry() + ":" + isoCountry + ":" + locale.getDisplayName()); byLocale.add(new MyLocale(locale)); } } // Roll them out of the set. ArrayList<String> list = new ArrayList<>(); for (MyLocale l : byLocale) { list.add(l.getCountry()); } return list; } public static void main(String[] args) throws InterruptedException { // Some demo usages. List<String> locales = listAll(); System.out.println(locales); } } 
+1
source share

In any case, I need to place the USA and the UK at the top of the list. The problem is that I cannot isolate the index or row that will be returned for the USA and the UK, because it is typical for the locale!

It looks like you should implement your own Comparator<Locale> to compare the two locales with the following steps:

  • If the locales match, return 0
  • If one language is USA, do it win
  • If one locale is UK, do it "win"
  • Otherwise, use o1.getDisplayCountry().compareTo(o2.getDisplayCountry()) (i.e. delegate existing behavior)

(This will put the United States before Great Britain.)

Then call Collections.sort instance of your custom comparator.

Do all this before extracting country names - then extract them from the sorted list.

+8
source share

yes, when you sort, just provide your own comparator

Collections.sort (worldCountriesByLocal, new Comparator () {

  @Override public int compare(String o1, String o2) { if (o1.equals(TOP_VALUE)) return -1; if (o2.equals(TOP_VALUE)) return 1; return o1.compareTo(o2); } }) 

where the top value will be the value of what you want always on top

+1
source share

I would write my POJO with a sorting marker consisting of integers that assign priority (for example, 0 for the US, 1 for the UK, 2 for everyone else), then some sort of separator, and then the name of the country. Then I would put an array in a HashMap with the key of this sort ID and POJO as val. Then I will select the keys from the map and repeat the sorting and get the name of the simple country for each sorted key.

eg.

 2.Sweden 2.France 2.Tanzania 0.US 1.UK 

sorts

 0.US 1.UK 2.France 2.Sweden 2.Tanzania 

EDIT: POJO is only necessary if you have more fields than the country name. If this is just a country name, I would set the sort ID as a hash key and the country name as val and skip the POJO part.

+1
source share

All Articles