Array element order is to reverse the instead,
Would like to invite bosses to disabuse, thank you very much,
Import the Java. IO. *;
import java.util.*;
Public class phototest {
Public static void main (String [] args) throws IOException {
ComparatorMycmp=new Comparator () {
Public int the compare (Integer a, Integer b) {
return 1;//array elements did not change
////return - 1 array elements to reverse a
}
};
The Integer [] d={9,8,10,90};
for(int i=0; I[I] System. Out. Print (d + "");
}
System.out.println();
The Arrays. Sort (d, mycmp);
for(int i=0; I[I] System. Out. Print (d + "");
}
System.out.println();
}
}
CodePudding user response:
You're going to order? Don't direct return, return a> It is good to bCodePudding user response:
I do not want to sort, I just want to test, if returned directly will work,,,CodePudding user response:
Compare the return value of the three kinds of circumstances, positive, zero, negative, when the return value is positive, said an lvalue (b) (a) is greater than the right values, in ascending order; When the return value is negative, it means the left value is less than the right value, will be carried out in descending order, and you don't have to determine the size of a and b returned directly, so the order of the array is the same when you return 1, return 1 when the array is reverse, so if you want to ascending array, you have to write this,ComparatorMycmp=new Comparator () {
Public int the compare (Integer a, Integer b) {
Return a & gt; b ? 1:1;
}
};
Want to descending wrote,
ComparatorMycmp=new Comparator () {
Public int the compare (Integer a, Integer b) {
Return a & lt; b ? 1:1;
}
};
CodePudding user response:
Thank you for your reply,And you don't have to judge the size of the a and b returned directly, so the order of the array is the same when you return 1, return 1 when the array is reverse
Here don't understand,,, here I do not judge returned directly, why return 1 the order of the array is the same?
CodePudding user response:
The building Lord did not say his doubt is what?The comparator's purpose is to return the size of the two elements,
If the landlord forced all the elements of the comparison of rules is the first element is greater than the second element, that is to say, direct return 1, so, after comparing with the comparator object sorting order, and sorting algorithm,
About sorting algorithms, there are 7 kinds of common, the original poster can pick a simple sorting algorithm, get your custom simulated experiment, the comparator will know exactly what's going on,
As for the upstairs said, the original collection of successively relationship does not change the conclusion, I don't agree with,
The result of a different sort algorithm, not necessarily the same,
For example,
After USES bubble sort, this collection order is arranged in reverse chronological order,
Using the result of the insertion sort sequence should be the same,
CodePudding user response:
The building Lord, forced return 1, order no change; Forced return 1, arranged in reverse chronological order, is to have a look at the bottom using JDK is what sort algorithm,A different version of the JDK sorting algorithm is adopted by the different,
Previous versions of the sorting algorithm, under the condition of less than seven elements, insertion sort, so is now and see the results of the building Lord,
Later versions of sorting algorithm, USES a binary sort algorithm, using the building after the custom of the comparator, binary tree degenerated into a linked list, so, as the original poster now,
But, this kind of result, is not universal, in other words, is that not all sorting algorithms, is such result,
CodePudding user response:
I just use your code to test, and found that it is the effect, the reason to know that you read the source code intercepting official interpretation of sorting methodNow your code is the method of sorting, insertion sort, insertion sort is to insert elements into an orderly array, the comparator has been returned because you are 1, so it was assumed that the element is orderly, will not perform any insertion sort code, we usually write a - b, when sorting, data, you now just need to perform an insert can achieve orderly, namely before inserted the 8 to 9
CodePudding user response:
Understand the underlying use insertion sort, looks not so slow to understand, we are doing the sorting, there are usually determine the size of the former and the latter, only meet the condition will perform a write code, only now judgment is the result of the comparator returns you yourself wrote, but now your comparator returns is a fixed value, that is to say, his verdict has been bigger than behind, in front of or behind the front than small, that he was executed, either don't move, or plug it to the front, keep repeat know traverse through all of the elements, but our comparator normal writing is a - b, the it at the time of comparison is negative, so through continuous insertion, change the position, is an orderly,