排序使用两个不同的标准的阵列 [英] Sorting an array using two different criteria
问题描述
我想用排序插入排序算法的阵列。该数组充满了 WordNode
,其中包括一个字
字段(从文本文件输入)元素和一个频率
字段(以测量特定字出现在文本文件的次数)。我已经实现了排序,这样的话是由频率排序(从最低到最高),但我也想按字母顺序排序,如果频率相等。我如何排序使用两个不同的标准,在同一时间?下面是我的排序code。
公共静态无效的排序(ArrayUnorderedList< WordNode>阵){
//创建插入排序堆
LinkedStack< WordNode>分类=新LinkedStack< WordNode>();
LinkedStack< WordNode>临时=新LinkedStack< WordNode>(); //而数组进行排序元素
而(!array.isEmpty()){
//删除数组当前元素
WordNode currentNode = array.removeFirst(); //而排序堆符合分类标准
而((sorted.isEmpty())及!及(sorted.peek()getFrequency()&所述; currentNode.getFrequency())){
//推动要素临时堆
temp.push(sorted.pop());
} //推电流元素进行排序堆
sorted.push(currentNode); //而临时叠层元件被替换
而(!temp.isEmpty()){
//推元素进行排序堆
sorted.push(temp.pop());
}
} //替换数组元素排序
而(!sorted.isEmpty()){
array.addToRear(sorted.pop());
}
}
AppClay的答案是绝对正确的,但如果你有兴趣整理起来,创建一个帮助实现的比较。
类WordNodeComparator实现比较< WordNode> {
@覆盖
公众诠释比较(WordNode LHS,RHS WordNode){
INT结果= lhs.getFrequency() - rhs.getFrequency();
如果(结果== 0){
返回lhs.getWord()的compareTo(rhs.getWord())。
}
其他{
返回结果;
}
}
}
然后你只需创建它的一个实例,并在循环使用它:
,而((sorted.isEmpty())及!及(nodeComparator.compare(sorted.peek(),currentNode)℃下)
这不仅使code更容易阅读和测试,它现在微不足道需要换出不同的比较器的实现。
I am trying to sort an array using the insertion sort algorithm. The array is filled with WordNode
elements that include a word
field (inputted from a text file) and a frequency
field (to measure the number of times the particular word appears in the text file). I have implemented the sort so that words are sorted by frequency (from lowest to highest), but I also want to sort alphabetically if frequencies are equal. How can I sort using two different criteria at the same time? Below is my sort code.
public static void sort(ArrayUnorderedList<WordNode> array) {
//create stacks for insertion sort
LinkedStack<WordNode> sorted = new LinkedStack<WordNode>();
LinkedStack<WordNode> temp = new LinkedStack<WordNode>();
//while the array has elements to be sorted
while(!array.isEmpty()) {
//remove current element from array
WordNode currentNode = array.removeFirst();
//while the sorted stack meets sorting criteria
while((!sorted.isEmpty()) && (sorted.peek().getFrequency() < currentNode.getFrequency())) {
//push elements to temp stack
temp.push(sorted.pop());
}
//push current element to sorted stack
sorted.push(currentNode);
//while the temp stack has elements to be replaced
while(!temp.isEmpty()) {
//push elements to sorted stack
sorted.push(temp.pop());
}
}
//replace sorted elements in array
while(!sorted.isEmpty()) {
array.addToRear(sorted.pop());
}
}
AppClay's answer is absolutely correct, but if you are interested in "tidying it up", create a helper that implements Comparator.
class WordNodeComparator implements Comparator<WordNode> {
@Override
public int compare(WordNode lhs, WordNode rhs) {
int result = lhs.getFrequency() - rhs.getFrequency();
if (result == 0) {
return lhs.getWord().compareTo(rhs.getWord());
}
else {
return result;
}
}
}
Then you simply create an instance of it, and use it in your loop:
while((!sorted.isEmpty()) && (nodeComparator.compare(sorted.peek(), currentNode) < 0)
Not only does this make the code easier to read and test, it's now trivial to swap out different Comparator implementations as needed.
这篇关于排序使用两个不同的标准的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!