2011-12-15 83 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; 
} 
相关问题