gpt4 book ai didi

java - 按子列表的值对 LIst 列表进行排序

转载 作者:行者123 更新时间:2023-12-02 12:46:38 26 4
gpt4 key购买 nike

private List<String> subList;
private List<List<String>> records = new ArrayList<List<String>>();

for(....){

subList = new ArrayList<String>();
...populate..
records.add(subList);
}

例如,subList 有三个字符串 - a、b 和 c。我想按 subList 中 b 的值对记录进行排序。

records at 0 has a list of "10", "20", "30"
records at 1 has a list of "10", "05", "30"
records at 2 has a list of "10", "35", "30"

排序后,记录的顺序应该是 -

records at 0 = records at 1 above
records at 1 = records at 0 above
records at 2 = records at 2 above

什么是一个好的算法?

最佳答案

类似于:

Collections.sort(records, new Comparator<List<String>>()
{
public int compare(List<String> o1, List<String> o2)
{
//Simple string comparison here, add more sophisticated logic if needed.
return o1.get(1).compareTo(o2.get(1));
}
})

虽然我发现对位置进行硬编码在实践中有点可疑,但您的意见可能会有所不同。

关于java - 按子列表的值对 LIst 列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1080030/

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