简体   繁体   English

无法按升序对列表进行排序

[英]Unable to sort the list by ascending order

Map<String, String> map ;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();


/////OnCreate.............


function1(){
map = new TreeMap<String, String>();
map.put("id", "id");
map.put("amont", "amount");

list.add(map);

System.out.println(list);
}

input values for id=1,3,5,57,80 id = 1,3,5,57,80的输入值

input values for amount=100,500,200,10,10000 金额的输入值= 100,500,200,10,10000

Unable to sort the list by ascending order of amount. 无法按金额的升序对列表进行排序。 It still shows in the order it was inserted. 它仍按插入顺序显示。

How do I fix this? 我该如何解决? I appreciate any help. 我感谢任何帮助。 Thanks in advance. 提前致谢。

Expected output: Ascending order of amount: 预期产出:金额升序:

amt=10 id=4  
amt=100 id=1  
amt=200 id=3  
amt=500 id=2  
amt=10000 id=5  

Assuming this is your input 假设这是你的输入

  Map<String, String> map ;
  List<Map<String, String>> list = new ArrayList<Map<String, String>>();
  map = new TreeMap<String, String>();
  map.put("id","1");
  map.put("amount","100");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","2");
  map.put("amount","500");  
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","3");
  map.put("amount","200");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","4");
  map.put("amount","10");
  list.add(map);
  map = new TreeMap<String, String>();
  map.put("id","5");
  map.put("amount","10000");
  list.add(map);

Here is your sorting code 这是您的排序代码

  Collections.sort(list, new Comparator<Map<String, String>>() {

        @Override
        public int compare(Map<String, String> o1, Map<String, String> o2) {
            String value1 =  o1.get("amount");
            String value2 =  o2.get("amount");
            return Integer.parseInt(value1)-Integer.parseInt(value2);
        }
    });

    for (Map<String, String> map1 : list) {
        String id = map1.get("id");
        String amount = map1.get("amount");
        System.out.println("amount= "+amount + " , " +"id = "+id);
    }

Output 产量

amount= 10 , id = 4
amount= 100 , id = 1
amount= 200 , id = 3
amount= 500 , id = 2
amount= 10000 , id = 5

update 更新

Replace return Integer.parseInt(value1)-Integer.parseInt(value2); 替换return Integer.parseInt(value1)-Integer.parseInt(value2); with the following code if the values are decimal. 如果值为十进制,则使用以下代码。

return Double.valueOf(value1).compareTo(Double.valueOf(value2));

Use sort() 使用sort()

Ex: 例如:

list.add(map);
Collections.sort(list)

System.out.println(list)

it will now print the list in ascending order of the type of content it contains. 它现在将按照其包含的内容类型的升序打印列表。

No.. You can't short any map according value using default sort method. 否..您不能使用默认排序方法根据值缩短任何地图。 You should write your custom method for sorting. 您应该编写自定义方法进行排序。 see this link- TreeMap sort by value 看到这个链接 - TreeMap按值排序

and I suggest use treemap if you want sorted by key.. 如果你想按键排序,我建议使用treemap。

Thanks! 谢谢!

You have to use custom Comparator and pass it in Treemap's constructor. 您必须使用自定义Comparator并在Treemap的构造函数中传递它。 For example to sort it with amount use following comparator : 例如,按照比较器使用金额进行排序:

Refer the following link. 请参阅以下链接。 It will definitely solve your problem 它肯定会解决你的问题

http://java2novice.com/java-collections-and-util/treemap/comparator-user-object/ http://java2novice.com/java-collections-and-util/treemap/comparator-user-object/

By default the list is not sorted. 默认情况下,列表未排序。 You need Collections.sort() method that takes Comparator . 你需要带有Comparator的 Collections.sort()方法。 So looks like you want to sort by amount , you should implement the comparator like below. 所以看起来你想按amount排序,你应该像下面这样实现比较器。

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return new Integer(amount1).compareTo(new Integer(amount2));
    }
});

Here is the full working copy, 这是完整的工作副本,

List<Map<String, String>> list = new ArrayList<Map<String, String>>();

Map<String, String> map1 = new TreeMap<String, String>();
map1.put("id", "2");
map1.put("amount", "200");

Map<String, String> map2 = new TreeMap<String, String>();
map2.put("id", "1");
map2.put("amount", "100");

Map<String, String> map3 = new TreeMap<String, String>();
map3.put("id", "3");
map3.put("amount", "300");

list.add(map1);
list.add(map2);
list.add(map3);

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        String amount1 = o1.get("amount");
        String amount2 = o2.get("amount");
        return amount1.compareTo(amount2);
    }
});

System.out.println(list);

It should print, 它应该打印,

[{amount=100, id=1}, {amount=200, id=2}, {amount=300, id=3}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM