2011-12-15 195 views
3

使用集合進行排序很漂亮,對我來說比使用Comparator好得多,因爲我有多個相同的值,我寧願他們不要扔到垃圾箱裏。但集合有它自己的問題,它似乎認爲重複2+組的數量小於其實際較小的計數器部件使用集合進行排序

示例具有這些鍵和值(「katy 1」,「mark 9」,「john 2" , 「愛麗絲11」, 「西亞22」, 「克里斯44」),它對其進行排序爲9

相反如下

愛麗絲11 凱特1 約翰2 西亞22 克里斯44 標記的正確順序 katy 1 john 2 mark 9 alice 11 josiah 22 mark 44

我該如何解決這個問題?

回答

4

既然你傳遞字符串,收集無告訴你如何解釋這些字符串的方式(即按字符串中的數字排序)。你必須更加明確。

您有兩種基本選擇:

選項1:創建一個新的數據類型來封裝名稱和數量,並實現由數量比較:

public class Person implements Comparable<Person> { 

    private String name; 
    private int number; 

    public Person(String name, int number) { 
     this.name = name; 
     this.number = number; 
    } 

    public int compareTo(Person p) { 
     if(this.number > p.number) return 1; 
     if(this.number < p.number) return -1; 
     return 0; 
    } 
} 

然後:

List<Person> persons = new ArrayList<Person>(); 
persons.add(new Person("alice", 11)); 
persons.add(new Person("katy", 1)); 
// etc. 
Collections.sort(persons); 

選項2:將字符串轉換爲鍵值對並將其放入TreeMap,它會自動保持通過鍵排序的值:

TreeMap<Integer, String> map = new TreeMap<Integer, String>(); 
map.put(11, "alice"); 
map.put(1, "katy"); 
// etc. 
0

最好的選擇是重構你的代碼來分離字符串和整數。

如果你不能或不想要你,你必須提供你自己的比較器。像

@Override 
public int compare(String o1, String o2) { 
    Integer i1 = Integer.parseInt(o1.replaceAll("[^0-9]", "")); 
    Integer i2 = Integer.parseInt(o2.replaceAll("[^0-9]", "")); 
    return i1.compareTo(i2); 
} 

東西然後你可以使用Collections.sort(List, Comparator)

List<String> list; // ... 
Collections.sort(list, new YourComparator()); 
0

你需要編寫自己的比較。如果你想比較一個字符串作爲一個數字,你需要將它轉換爲一個數字。否則,「22」 <「4」,即使22> 4.

不過,我看不出你如何讓使用默認的比較器的第一級。

1

我認爲你必須創建Person類,它實現可比接口

class Person implements Comparable<Person >{ 

     String name; 
     Integer number; 
     public int compareTo(Person o) { 

     return number.compareTo(o.number); 
    } 

} 
0

檢查這個例子

編輯

public static void main(String arg[]){ 

    List<String> l = Arrays.asList(new String[]{"katy 1","mark 9","john 2","alice 11","josiah 22","chris 44"}); 

    Collections.sort(l, new Comparator<String>() { 
     public int compare(String x, String y) { 
      Integer a = Integer.parseInt(x.substring(x.indexOf(" ")).trim()); 
      Integer b = Integer.parseInt(y.substring(y.indexOf(" ")).trim()); 
      return a.compareTo(b); 
     } 
    }); 
    System.out.println(l.toString()); 
} 
+1

爲什麼這麼複雜? Integer類完全能夠執行自己的比較 – 2011-12-15 10:30:06

+0

@JohanSjöberg是的,你是對的。其實我不知道這個感謝清除概念 – Pratik 2011-12-15 10:34:35

2
  1. Store中的數據爲Map<String, Integer> - 不要臨時抱佛腳兩種數據類型轉換爲一個字符串。
  2. 拿到項設置成一個列表,並對其進行排序
  3. 把分類項設置成一個有序圖

下面是一些代碼,將做到這一點:

public static void main(String[] args) { 
    // Set up and load the map 
    Map<String, Integer> nameAgeMap = new HashMap<String, Integer>(); 
    nameAgeMap.put("katy", 1); 
    nameAgeMap.put("chris", 44); 
    nameAgeMap.put("alice", 11); 
    nameAgeMap.put("josiah", 22); 
    nameAgeMap.put("john", 2); 

    // Create-and-load a List of entries 
    List<Map.Entry<String, Integer>> entries = new ArrayList<Map.Entry<String, Integer>>(nameAgeMap.entrySet()); 
    // Sort the list using a custom Comparator that compares the ages 
    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() { 
     public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { 
      return o1.getValue().compareTo(o2.getValue()); 
     }}); 

    // Load the entries into a Map that preserves insert order 
    Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); 
    for (Map.Entry<String, Integer> entry : entries) 
     sortedMap.put(entry.getKey(), entry.getValue()); 

    // All done - let's see what we got 
    System.out.println(sortedMap); 
} 

輸出:

{katy=1, john=2, alice=11, josiah=22, chris=44} 
1

按$升序對邏輯值進行排序。如果您需要它降序請交換變量i1和i2

public static void main(String[] args) { 



    List<String> l_oTestList = new ArrayList<String>(); 
    l_oTestList.add("$10000 - $12000"); 
    l_oTestList.add("$50 - $100"); 
    l_oTestList.add("$10000 - $12000"); 
    l_oTestList.add("$100 - $150"); 
    l_oTestList.add("$150 - $200"); 
    l_oTestList.add("$200 - $250"); 
    l_oTestList.add("$0 - $10"); 
    l_oTestList.add("$10 - $20"); 
    l_oTestList.add("$20 - $50"); 
    l_oTestList.add("$250 - $500"); 
    l_oTestList.add("$500 - $750"); 
    l_oTestList.add("$750 - $1000"); 
    l_oTestList.add("$1000 - $1250"); 
    l_oTestList.add("$1250 - $10000"); 
    List<String> l_oTestList1 = sort(l_oTestList); 
    System.out.println(l_oTestList1.toString()); 
} 

private static List<String> sort(List<String> pTestList) { 
    Collections.sort(pTestList, new Comparator<String>() { 
     public int compare(String o1, String o2) { 
      Integer i1 = Integer.parseInt(o1.replace("$", "").substring(0,o1.indexOf("-")-2).trim()); 
      Integer i2 = Integer.parseInt(o2.replace("$", "").substring(0,o2.indexOf("-")-2).trim()); 
      return (i2 > i1 ? -1 : (i2 == i1 ? 0 : 1)); 
     } 
    }); 
    return pTestList; 
} 
相關問題