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));
}
})
尽管我发现对位置进行硬编码在实践中有点可疑,但您的意见可能会有所不同。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)