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

我有一个像这样的数据集:


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]

...

问题:如何根据字符串列表对整数列表进行排序?


潇潇雨雨
浏览 168回答 3
3回答

慕标5832272

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

白衣染霜花

如果你这样做,接受的答案建议:wonders.sort(Comparator.comparingInt(Wonder::getHostility).reversed()&nbsp; &nbsp; .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]&nbsp;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().&nbsp; &nbsp; thenComparing(wonder -> wonder.getCountries().get(0)));&nbsp;然后你会得到:95 [Antarctica]95 [Italy]95 [Nepal, Tibet]91 [Israel, Jordan]85 [France, Italy]&nbsp;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

冉冉说

不确定我明白你的问题是什么。下面的伪代码能解决您的问题吗?@Overridepublic int compareTo(Wonder other) {&nbsp; &nbsp; if(hostility == other.hostility) {&nbsp; &nbsp; &nbsp; &nbsp;// let's compare the strings list when hostility integer are equals (84, 84)&nbsp; &nbsp; &nbsp; &nbsp;String firstOtherCountry = other.countries.SortAlphabetically().get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;// we sort the countries list for current line and other wonder&nbsp; &nbsp; &nbsp; &nbsp;// you compare alphabetically first element of each list :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;// return 1, 0 or -1 here.&nbsp; &nbsp; } else if(hostility < other.hostility) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; } else if(hostility > other.hostility) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java