java 使用自定义排序对字符串数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16541314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting an array of String with custom ordering
提问by Sam
I have a String
array:
我有一个String
数组:
String[] str = {"ab" , "fog", "dog", "car", "bed"};
Arrays.sort(str);
System.out.println(Arrays.toString(str));
If I use Arrays.sort
, the output is:
如果我使用Arrays.sort
,输出是:
[ab, bed, car, dog, fog]
But I need to implement the following ordering:
但我需要实现以下顺序:
FCBWHJLOAQUXMPVINTKGZERDYS
FCBWHJLOAQUXMPVINTKGZERDYS
I think I need to implement Comparator
and override compare
method:
我想我需要实现Comparator
和覆盖compare
方法:
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return 0;
}
});
How should I go about solving this?
我应该如何解决这个问题?
回答by Majid Laissi
final String ORDER= "FCBWHJLOAQUXMPVINTKGZERDYS";
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return ORDER.indexOf(o1) - ORDER.indexOf(o2) ;
}
});
You can also add:
您还可以添加:
o1.toUpperCase()
If your array is case in-sensitive.
如果您的数组不区分大小写。
Apparently the OP wants to compare not only letters but strings of letters, so it's a bit more complicated:
显然 OP 不仅要比较字母,还要比较字母串,所以它有点复杂:
public int compare(String o1, String o2) {
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
pos1 = ORDER.indexOf(o1.charAt(i));
pos2 = ORDER.indexOf(o2.charAt(i));
}
if (pos1 == pos2 && o1.length() != o2.length()) {
return o1.length() - o2.length();
}
return pos1 - pos2 ;
}
回答by Avi
I would do something like this:
我会做这样的事情:
Put the letters in a HashTable (let's call it orderMap). Key is the letter, value is the index in ORDER.
将字母放在 HashTable 中(我们称之为 orderMap)。Key 是字母,value 是 ORDER 中的索引。
And then:
接着:
Arrays.sort(str, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int length = o1.length > o2.length ? o1.length: o2.length
for(int i = 0; i < length; ++i) {
int firstLetterIndex = orderMap.get(o1.charAt(i));
int secondLetterIndex = orderMap.get(o2.charAt(i));
if(firstLetterIndex == secondLetterIndex) continue;
// First string has lower index letter (for example F) and the second has higher index letter (for example B) - that means that the first string comes before
if(firstLetterIndex < secondLetterIndex) return 1;
else return -1;
}
return 0;
}
});
For making it case-insensitive just do toUpperCase() to both strings at the beginning.
为了使其不区分大小写,只需在开头对两个字符串执行 toUpperCase() 即可。
回答by Mr.Nestorjava
Took my time to improve on the selected answer. This is more efficient
花时间改进所选的答案。这样效率更高
public static void customSort(final String order,String[] array){
String[] alphabets={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"};
String keyword=order;
for(int g=0; g<alphabets.length; g++){
String one=alphabets[g];
if(!keyword.toUpperCase().contains(one)){keyword=keyword+one;}
}
final String finalKeyword=keyword;
Arrays.sort(array, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int pos1 = 0;
int pos2 = 0;
for (int i = 0; i < Math.min(o1.length(), o2.length()) && pos1 == pos2; i++) {
pos1 = finalKeyword.toUpperCase().indexOf(o1.toUpperCase().charAt(i));
pos2 = finalKeyword.toUpperCase().indexOf(o2.toUpperCase().charAt(i));
}
if (pos1 == pos2 && o1.length() != o2.length()) {
return o1.length() - o2.length();
}
return pos1 - pos2 ;
}
});
//Arrays.sort(array, Collections.reverseOrder());
}
回答by Jacek
Here you can find usefull link:
在这里您可以找到有用的链接:
Using comparator to make custom sort
In your example instead comparing specific attributes of class you nedd to check possition of char in benchmarked String and base on this check if it's greather/equal/smaller.
在您的示例中,而是比较您需要检查基准字符串中 char 位置的类的特定属性,并基于此检查它是否更大/相等/更小。