Java:以数字方式对字符串数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7348223/
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
Java: Sort a String array in a numeric way
提问by user934779
I have a String array that contains the following entries:
我有一个包含以下条目的字符串数组:
Array[0] = "70% Marc"
Array[1] = "50% Marc"
Array[2] = "100% Marc"
Array[3] = "20% Marc"
And I would like to sort this array descending.
When I use Arrays.sort(Array)
then it does sort it descending but the 100% Marc
is at the bottom (because it only looks at the first character to sort it). I want it to be sorted like this:
我想对这个数组进行降序排序。当我使用Arrays.sort(Array)
then 时,它确实降序排序,但100% Marc
is 在底部(因为它只查看第一个字符进行排序)。我希望它像这样排序:
"100% Marc"
"70% Marc"
"50% Marc"
"20% Marc"
How can I do that?
我怎样才能做到这一点?
回答by Bala R
Write your own CustomStringComparator
and use it with the sort method.
编写您自己的CustomStringComparator
并将其与 sort 方法一起使用。
public class CustomStringComparator implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
// extract numeric portion out of the string and convert them to int
// and compare them, roughly something like this
int num1 = Integer.parseInt(str1.substring(0, str1.indexOf("%") - 1));
int num2 = Integer.parseInt(str2.substring(0, str2.indexOf("%") - 1));
return num1 - num2;
}
}
回答by Swaranga Sarma
You have to use a custom Comparator. Basically in your method compare()
you will write the logic to order two String
s.
您必须使用自定义Comparator。基本上在您的方法中,compare()
您将编写逻辑来订购两个String
s。
回答by David Moreno García
You will need a custom comparator:
您将需要一个自定义比较器:
import java.util.Comparator;
public class CustomComparator implements Comparator<String>{
@Override
public int compare(String firstString, String secondString) {
int number1 = Integer.parseInt(firstString.substring(0, firstString.indexOf('%') - 1);
int number2 = Integer.parseInt(secondString.substring(0, secondString.indexOf('%') - 1);
return number1.compareTo(number2);
}
}
Use this comparator with something like this:
将此比较器用于以下内容:
List<String> entries = new ArrayList<String>();
entries.add("70% Marc");
entries.add("50% Marc");
entries.add("100% Marc");
entries.add("20% Marc");
CustomComparator comparator = new CustomComparator();
Collections.sort(entries, comparator);
回答by Bozho
You'd need to implement a custom Comparator<String>
that handles the comparison by removing the percent sign:
您需要Comparator<String>
通过删除百分号来实现处理比较的自定义:
public int compare(String str1, String str2) {
Integer number1 = Integer.parseInt(str1.substring(0, str1.length - 1));
Integer number2 = Integer.parseInt(str1.substring(0, str2.length - 1));
return number1.compareTo(number2);
// or use primitives, and then: return (x < y) ? -1 : ((x == y) ? 0 : 1);
// but that's harder to read
}
The comparison itself can be done by using the Integer
wrapper, the code that I pasted (taken from the wrapper), or guava's Ints.compare(int1, int2)
比较本身可以通过使用Integer
包装器、我粘贴的代码(取自包装器)或番石榴的Ints.compare(int1, int2)
Then use Collections.sort(array, comparator)
然后使用 Collections.sort(array, comparator)
回答by dougajmcdonald
It's not looking at the first character, it's sorting based on a string value as that's what you have in your array.
它不是查看第一个字符,而是基于字符串值进行排序,因为这就是数组中的内容。
I would suggest storing two fields,
我建议存储两个字段,
Array[0,0] = 50; Array[0,1] = "Marc"
数组[0,0] = 50; 数组[0,1] = "马克"
And then sorting based on the numeric field, if that's the behaviour you're after.
然后根据数字字段进行排序,如果这是您所追求的行为。