gpt4 book ai didi

java - 根据键值对对象进行排序

转载 作者:行者123 更新时间:2023-12-04 04:54:20 25 4
gpt4 key购买 nike

我有一个包含 CricketLiveScore 对象的 ArrayList

  CricketLiveScore{
String homeAbbrevation;
}

List myList=new ArrayList<CricketLiveScore>();
CricketLiveScore c1=new CricketLiveScore();
c1.setHomeAbbrevation("IND vs PAK");
CricketLiveScore c2=new CricketLiveScore();
c2.setHomeAbbrevation("SRI vs NWZ");
CricketLiveScore c3=new CricketLiveScore();
c3.setHomeAbbrevation("AUS vs BAN");
CricketLiveScore c4=new CricketLiveScore();
c4.setHomeAbbrevation("NWZ vs IND");

list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);

我需要对这个基于对象的 Arraylist 进行排序,并在家庭缩写中使用值 IND
有人可以对此提供帮助,以便我需要对列表进行排序并首先显示包含 IND 值的输出。

最佳答案

试试这个:

public class CricketLiveScoreComparator implements Comparator<CricketLiveScore> {
@Override
public int compare(CricketLiveScore c1, CricketLiveScore c2) {
return c1.getHomeAbbrevation().compareTo(c2.getHomeAbbrevation());
}
}

然后调用排序操作:
Collections.sort(myList, new CricketLiveScoreComparator());

关于java - 根据键值对对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16984706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com