2012-10-05 29 views
0

我創建了以下方法:如何重新排序列表<String>

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; 
} 

它相當簡單,它返回世界各國的用戶列表中的語言環境。然後,我將其排序以使其符合字母。這一切都完美的作品(除了我似乎偶爾會國家的副本!)。

無論如何,我需要的是把美國和英國在列表的頂部不管。我的問題是,因爲這是特定於語言環境,我不能隔離指數或將在美國和英國被返回的字符串!

任何想法將非常感激。

+0

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Locale.html#getCountry() – MikeTheLiar

回答

1

您也可以使用TreeSet消除重複項和您自己的Comparator,以使US和GB一開始就可以使用。

您正在獲取重複項(這將消除),因爲每個國家/地區通常有多個區域設置。有一個美國(西班牙)以及一個美國(英語),例如有三個瑞士(法語,德語和意大利語)。

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); 
    } 
} 
8

無論如何,我需要的是將美國和英國列入榜首。我的問題是,因爲這是特定於語言環境,我不能隔離指數或將在美國和英國被返回的字符串!

這聽起來像你應該實現自己的Comparator<Locale>兩個區域設置下面的步驟比較:

  • 如果語言環境是相同的,則返回0
  • 如果一個語言環境是美國,使那「贏」
  • 如果一個區域是英國,作出這樣的「雙贏」
  • 否則,使用o1.getDisplayCountry().compareTo(o2.getDisplayCountry())(即委託給現有的行爲)

(這將使美國英國之前。)

然後調用Collections.sort與您的自定義比較器的一個實例。

在之前完成所有提取國家名稱 - 然後從排序列表中提取它們。

1

是,當你做排序,只需提供自己的比較

Collections.sort(worldCountriesByLocal,新的比較(){

@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); 
    } 
}) 

其中頂部值將是值始終要什麼頂部

1

我會寫我自己的POJO帶有幾分令牌由一個整數分配優先級(如0美國,1英國,2其他人)的,然後一些分隔符,然後是國家的名字,然後我會放由該排序ID和t鍵入的HashMap中的數組他POJO作爲val。然後,我會出來的圖鍵排序,通過排序迭代和每個分類檢索關鍵平原國家名稱。

E.g.

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

各種

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

編輯:需要一個POJO只有當你有比國名等多個領域。如果只是國家名稱,我會將排序標識設置爲散列鍵和國家/地區名稱作爲val並跳過POJO部分。