我的输入 -
List<String> parameterNames => [value0, type1, type0, name1, value1, name0]
我使用 Collections.Sort
Collections.sort(parameterNames)
我得到这样的答案
[name0, name1, type0, type1, value0, value1]
我想像这样排序并获取列表
[name0,type0,value0,name1,type1,value1]
我能用 Java 做到这一点吗?
最佳答案
编写自定义Comparator ,并将其实例传递给 sort
方法:
Comparator<String> myComparator = new Comparator<String>() {
public int compare(String str1, String str2) {
// get the number at the end, and compare on the basis of that number
// And then do the string comparison on substring before number
}
};
Collections.sort(parameterNames, myComparator);
关于java - 按编号对列表排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178932/