通过子列表的值对LIst列表排序 [英] Sorting a List of LIst by the Value of the sublist
本文介绍了通过子列表的值对LIst列表排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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。
我想通过子列表中的值b对记录进行排序。
For example, subList has three Strings - a, b, and c. I want to sort the records by the value of b in subList.
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"
排序后,记录的顺序应为 -
After the sort, the order of records should be -
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));
}
})
我发现硬编码的位置在实践中有点怀疑,你的意见可能会有所不同。
Though I find hard-coding the positions a little dubious in practice, your opinion may differ.
这篇关于通过子列表的值对LIst列表排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文