为了账号安全,请及时绑定邮箱和手机立即绑定

根据Java中的字符串列表对整数列表进行排序

根据Java中的字符串列表对整数列表进行排序

潇潇雨雨 2023-05-10 17:04:27
我有一个像这样的数据集:85 [Italy, France]    95 [Italy]91 [Israel, Jordan]85 [France, Italy, Switzerland]80 [USA]84 [Mongolia, China]95 [Antarctica]84 [African Union]82 [Argentina]95 [Tibet, Nepal]...我使用下面的代码(定义类模型)基于整数进行了排序:public class Wonder implements Comparable<Wonder> {    int hostility;    List<String> countries;    //some other data members    //constructor    //getters    @Override    public int compareTo(Wonder other) {        if(hostility == other.hostility) {            return 0;        } else if(hostility < other.hostility) {            return -1;        } else if(hostility > other.hostility) {            return 1;        } else {            return 0;        }    }}排序代码(PS:getAllData方法将返回奇迹列表,从文本文件加载):List<Wonder> wonders = getAllData(filePath);wonders.sort((c1,c2)->c1.compareTo(c2));Collections.reverse(wonders); // ordering highest to lowest 排序后的数据集(基于整数排序)看起来像这样:95 [Antarctica]95 [Italy]95 [Tibet, Nepal]91 [Israel, Jordan]85 [France, Italy, Switzerland]85 [Italy, France]84 [Mongolia, China]84 [African Union]82 [Argentina]80 [USA]...现在,需要将新生成的数据集按字母顺序排序,即国家列表(字符串)。例如,在新数据集中,有两条记录具有相同的整数 84(第一个整数为蒙古国,第二个整数为非洲联盟国家),因此第二条记录应该排在第一位,因为非洲联盟按字母顺序排列在蒙古之前。...84 [African Union]84 [Mongolia, China]...问题:如何根据字符串列表对整数列表进行排序?
查看完整描述

3 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

您可以进一步专门化 compareTo 函数以进行二次比较。我假设每个列表至少包含一个国家;如果不是这种情况,您必须处理空列表。改变后的 compareTo 是这样的:


@Override

public int compareTo(Wonder other) {

    if(this == other) {

        return 0;

    } else if(hostility < other.hostility) {

        return -1;

    } else if(hostility > other.hostility) {

        return 1;

    } else {

        return -countries.get(0).compareTo(other.countries.get(0));

    }

}

或者你可能正在寻找这个:


wonders.sort(Comparator.comparingInt(Wonder::getHostility).reversed()

    .thenComparing(wonder -> wonder.getCountries().get(0)));

//don't reverse afterwards!

查看完整回答
反对 回复 2023-05-10
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

如果你这样做,接受的答案建议:


wonders.sort(Comparator.comparingInt(Wonder::getHostility).reversed()

    .thenComparing(wonder -> wonder.getCountries().get(0)));

在您提供的文本文件上,您将获得下一个结果:


95 [Antarctica]

95 [Italy]

95 [Tibet, Nepal]

91 [Israel, Jordan]

85 [France, Italy, Switzerland]

85 [Italy, France]

84 [African Union] 

84 [Mongolia, China]

82 [Argentina]

80 [USA]

70 [Australia]

69 [Japan]

69 [USA, Canada]

65 [The Hawaiian Islands]

65 [USA]

55 [Russia]

50 [Brazil, Argentina]

19 [Tanzania]

17 [Northern Ireland]

16 [China]

12 [African Union]

10 [Australia]

10 [Brazil]

2 [USA]

但是,如果您先排序countries然后接受答案:


wonders.forEach(wonder -> Collections.sort(wonder.getCountries()));

wonders.sort(Comparator.comparingInt(Wonder::getHostility).reversed().

    thenComparing(wonder -> wonder.getCountries().get(0))); 

然后你会得到:


95 [Antarctica]

95 [Italy]

95 [Nepal, Tibet]

91 [Israel, Jordan]

85 [France, Italy] 

85 [France, Italy, Switzerland]

84 [African Union]

84 [China, Mongolia]

82 [Argentina]

80 [USA]

70 [Australia]

69 [Canada, USA]

69 [Japan]

65 [The Hawaiian Islands]

65 [USA]

55 [Russia]

50 [Argentina, Brazil]

19 [Tanzania]

17 [Northern Ireland]

16 [China]

12 [African Union]

10 [Australia]

10 [Brazil]

2 [USA]

注意这两个列表中的hostility值85和值69。顺序不一样。不知道这是否与您有关。


PS 如果你实施Comparable#compareTo(),你也应该实施,equals()因为它们之间有合同:


(x.compareTo(y) == 0) == (x.equals(y))

如果不是这种情况,您应该注意:This class has a natural ordering that is inconsistent with equals.


最后一件事:


compareTo()NullPointerException如果当前对象与对象进行比较,null而不是在这种情况下equals()返回,则必须抛出。false


查看完整回答
反对 回复 2023-05-10
?
冉冉说

TA贡献1877条经验 获得超1个赞

不确定我明白你的问题是什么。下面的伪代码能解决您的问题吗?


@Override

public int compareTo(Wonder other) {

    if(hostility == other.hostility) {

       // let's compare the strings list when hostility integer are equals (84, 84)

       String firstOtherCountry = other.countries.SortAlphabetically().get(0);           

       // we sort the countries list for current line and other wonder

       // you compare alphabetically first element of each list : 

       // return 1, 0 or -1 here.

    } else if(hostility < other.hostility) {

        return -1;

    } else if(hostility > other.hostility) {

        return 1;

    } else {

        return 0;

    }

}


查看完整回答
反对 回复 2023-05-10
  • 3 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信