我的意见 -
List<String> parameterNames => [value0, type1, type0, name1, value1, name0]
Run Code Online (Sandbox Code Playgroud)
我使用Collections.Sort
Collections.sort(parameterNames)
Run Code Online (Sandbox Code Playgroud)
我得到这样的答案
[name0, name1, type0, type1, value0, value1]
Run Code Online (Sandbox Code Playgroud)
我想排序并获得这样的列表
[name0,type0,value0,name1,type1,value1]
Run Code Online (Sandbox Code Playgroud)
我能用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);
Run Code Online (Sandbox Code Playgroud)