[英]Sorting an array alphabetically
I have an array which i need to sort its elements by occurrence then alphabetically. 我有一个数组,我需要按字母顺序排列其元素。 For example: 例如:
55 The
32 ASomething
32 BSomething
ASomething should come before Bsomething because:
1) they have the same number
2) A comes before B alphabetically
So you sort first by the number of occurrence then Alphabetically 因此,您首先按出现次数排序,然后按字母顺序排序
What is the best way to do that. 什么是最好的方法。 I am using merge sort to sort the counts but how do I put a statement that it will check if they have the same number, it sorts alphabetically (could be more than 2 words). 我使用合并排序来对计数进行排序,但是我如何设置一个声明,它将检查它们是否具有相同的数字,它按字母顺序排序(可能超过2个单词)。
SOLUTION: What I did is a merge sort on the data before I did a merge sorts on the counts of data and that was good enough :) Thanks everyone for the help 解决方案:我做的是在对数据进行合并排序之前对数据进行合并排序,这非常好:)感谢大家的帮助
You need a custom Comparator
for that using Arrays.sort()
: 使用Arrays.sort()
需要一个自定义Comparator
:
Arrays.sort(array, new CustomComparator());
public class CustomComparator implements Comparator<String> {
private final Pattern pattern = Pattern.compile("(\\d+)\\s+(.*)");
public int compare(String s1, String s2) {
Matcher m1 = pattern.matcher(s1);
if (!m1.matches()) {
throw new IllegalArgumentException("s1 doesn't match: " + s1);
}
Matcher m2 = pattern.matcher(s2);
if (!m2.matches()) {
throw new IllegalArgumentException("s2 doesn't match: " + s2);
}
int i1 = Integer.parseInt(m1.group(1));
int i2 = Integer.parseInt(m2.group(1));
if (i1 < i2) {
return 1;
} else if (i1 > i2) {
return -1;
}
return m1.group(2).compareTo(m2.group(2));
}
}
For Collections
you can use Collections.sort()
对于Collections
您可以使用Collections.sort()
The above assumes your array elements are String
s like "22 ASomething"
rather than a specific data structure containing occurrences and some text. 上面假设您的数组元素是String
如"22 ASomething"
而不是包含出现次数和一些文本的特定数据结构。 If that is the case you can use a simpler Comparator
. 如果是这种情况,您可以使用更简单的Comparator
。
Also if you do have an array of String
s it might be worth first transforming it into an array of objects that have been parsed to save over-parsing the elements (ie some elements will be parsed more than once). 此外,如果你有一个String
数组,可能需要先将它转换为一个已解析的对象数组,以保存过度解析元素(即一些元素将被解析多次)。
You should ensure that the sorting algorithm you are using guarantees "stability" as does java.util.Collections.sort : 您应该确保您使用的排序算法与java.util.Collections.sort一样保证“稳定性”:
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. 这种类型保证是稳定的:相同的元素不会因排序而重新排序。
You make no mention of what data structure you are using which would definitely guide your approach. 您没有提到您使用的数据结构,这肯定会指导您的方法。 For example, you might use a Map> to model your data in which case it would make sense to sort the Lists and then iterate through the ordered keys of the Map. 例如,您可以使用Map>对数据建模,在这种情况下,对列表进行排序然后迭代Map的有序键是有意义的。 This would not require a custom Comparator. 这不需要自定义Comparator。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.