繁体   English   中英

基于Java中的字符串列表对整数列表进行排序

[英]Sorting a List of integers based on a List of strings in 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]
...

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

您可以进一步专门化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!

根据@Andrew的风格

有最好的答案的repl.it

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

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]

注意这两个列表中的值为8569 hostility 订单不一样。 不知道这是否与您相关。

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

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

@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;
    }
}

如何按字母顺序对列表进行排序?

暂无
暂无

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

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