在 java 中使用值对象内的阿拉伯值对地图进行排序

sort map using arabic value inside value object in java

我正在尝试对地图进行排序。我的地图具有作为整数的键和作为 dto 对象的值。但是,问题来了。 dto 对象将具有国家的阿拉伯文描述。因此我想使用阿拉伯名称进行排序。我尝试使用

@Override
public int compare(String s1, String s2) {
    if(map.get(s1) >= map.get(s2)){
        return -1;
    }else{
        return 1;
    }   
}

但是,这仅适用于字符串对象。

我的实际代码是

        Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();

    NationalityDto n2 = new NationalityDto();
    n2.setNatid(102);
    n2.setDesc("الهند");
    m.put(102, n2);
    NationalityDto n3 = new NationalityDto();
    n3.setNatid(103);
    n3.setDesc("سعودي");
    m.put(103, n3);
    NationalityDto n1 = new NationalityDto();
    n1.setNatid(101);
    n1.setDesc("مصر");
    m.put(101, n1);
    NationalityDto n4 = new NationalityDto();
    n4.setNatid(104);
    n4.setDesc("الكويت");
    m.put(104, n4);
    NationalityDto n5 = new NationalityDto();
    n5.setNatid(105);
    n5.setDesc("لبنان");
    m.put(105, n5);
    System.out.println(m);

更新代码

public class TestArabic {
public static void main(String[] args) {

    Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();

    NationalityDto n2 = new NationalityDto();
    n2.setNatid(102);
    n2.setDesc("الهند");
    m.put(102, n2);
    NationalityDto n3 = new NationalityDto();
    n3.setNatid(103);
    n3.setDesc("سعودي");
    m.put(103, n3);
    NationalityDto n1 = new NationalityDto();
    n1.setNatid(101);
    n1.setDesc("مصر");
    m.put(101, n1);
    NationalityDto n4 = new NationalityDto();
    n4.setNatid(104);
    n4.setDesc("الكويت");
    m.put(104, n4);
    NationalityDto n5 = new NationalityDto();
    n5.setNatid(105);
    n5.setDesc("لبنان");
    m.put(105, n5);
    System.out.println(m);

    Map sortedMap = sortByValue(m);




}

private static Map sortByValue(Map m) {
    Map sortedMap = new TreeMap(new ValueComparator(m));
    sortedMap.putAll(m);
    return sortedMap;
}
}

class ValueComparator implements Comparator {
Map map;

public ValueComparator(Map map) {
    this.map = map;
}

@Override
public int compare(String s1, String s2) {
    return map.get(s1).getDesc().compareTo(map.get(s2).getDesc());
}


}

class ValueComparator 不允许使用带有字符串和字符串参数的比较方法。代码有什么问题?

好的,这里是完整的代码。

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class TestArabic {
public static void main(String[] args) {

    Map<Integer, NationalityDto> m = new HashMap<Integer, NationalityDto>();

    NationalityDto n2 = new NationalityDto();
    n2.setNatid(102);
    n2.setDesc("الهند");
    m.put(102, n2);
    NationalityDto n3 = new NationalityDto();
    n3.setNatid(103);
    n3.setDesc("سعودي");
    m.put(103, n3);
    NationalityDto n1 = new NationalityDto();
    n1.setNatid(101);
    n1.setDesc("مصر");
    m.put(101, n1);
    NationalityDto n4 = new NationalityDto();
    n4.setNatid(104);
    n4.setDesc("الكويت");
    m.put(104, n4);
    NationalityDto n5 = new NationalityDto();
    n5.setNatid(105);
    n5.setDesc("لبنان");
    m.put(105, n5);
    System.out.println(m);

    Map sortedMap = sortByValue(m);


    System.out.println(sortedMap);

}

private static Map sortByValue(Map m) {
    Map<Integer, NationalityDto> sortedMap = new TreeMap(new ValueComparator(m));
    sortedMap.putAll(m);
    return sortedMap;
}
}

class ValueComparator implements Comparator<Integer> {
Map<Integer, NationalityDto> map;

public ValueComparator(Map map) {
    this.map = map;
}

public int compare(Integer s1, Integer s2) {
    // TODO Auto-generated method stub
    return ((NationalityDto) map.get(s1)).getDesc().compareTo(((NationalityDto) map.get(s2)).getDesc());
}
}

希望对你有所帮助