如何按升序对Map字符串列表进行排序

时间:2015-07-23 14:08:29

标签: java sorting

我有一个HashMap列表,我想在Java中按升序对其进行排序。

我使用自定义Comparator来实现逻辑,但它失败了。它只是根据Map键对列表进行分组。它没有完全排序列表。

我的代码看起来像 -

public class ListMap {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        List<HashMap<String, String>> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            int value = randInt(1, 20);
            long timeInMillis = cal.getTimeInMillis();
            HashMap<String, String> mp = new HashMap<>();
            mp.put("" + value, "Values" + value + ":" + timeInMillis);
            list.add(mp);
        }

        Collections.sort(list, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> map1,
                    Map<String, String> map2) {
                String val1 = "";
                String val2 = "";
                for (Map.Entry<String, String> entry : map1.entrySet()) {
                    val1 = entry.getKey();
                }
                for (Map.Entry<String, String> entry : map2.entrySet()) {
                    val2 = entry.getKey();
                }
                return val1.compareTo(val2);
            }
        });

        for (Map<String, String> hashMap : list) {
            for (Map.Entry<String, String> entry : hashMap.entrySet()) {
                System.out.println(entry.getKey() + "/" + entry.getValue());
            }
        }

    }

    public static int randInt(int min, int max) {
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

}

打印输出 -

10/Values10:1437660285301
11/Values11:1437660285301
11/Values11:1437660285301
11/Values11:1437660285301
13/Values13:1437660285301
13/Values13:1437660285301
15/Values15:1437660285301
15/Values15:1437660285301
15/Values15:1437660285301
16/Values16:1437660285301
17/Values17:1437660285301
18/Values18:1437660285301
18/Values18:1437660285301
19/Values19:1437660285301
19/Values19:1437660285301
2/Values2:1437660285301
3/Values3:1437660285301
4/Values4:1437660285301
6/Values6:1437660285301
6/Values6:1437660285301

它按键对值进行分组,但我想要整个排序列表,如上例所示。

我期待2,然后是3,然后是4,继续。

2 个答案:

答案 0 :(得分:4)

您正在根据String类型的键进行比较。比较将按字典顺序进行,即12小于2

您需要在比较器中将键比较为Integers

Collections.sort(list, new Comparator<Map<String, String>>() {
    @Override
    public int compare(Map<String, String> map1,
             Map<String, String> map2) {
        String val1 = "";
        String val2 = "";
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            val1 = entry.getKey();
        }
        for (Map.Entry<String, String> entry : map2.entrySet()) {
            val2 = entry.getKey();
        }

        return Integer.valueOf(val1).compareTo(Integer.valueOf(val2));
    }
});

答案 1 :(得分:0)

只需修改比较器,将键字符串转换为整数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<GridItem> data = new List<GridItem>();


            for (int i = 0; i < 5; i++)
            {
                data.Add(new GridItem()
                    {
                        Text = "Item " + i
                    });

                data.Add(new GridItem()
                    {
                        Text = "ItemX " + i
                    });

            }


            this.dgGrid.ItemsSource = data;
        }

  class GridItem
    {
        public string Text
        {
            get;
            set;
        }
    }