I tried to understand this knowledge point all night. It is true that for the little java cainiao I just started learning,
In this way, the summary is even more happy ..
During this period of thinking, I have these questions.
1:
How to sort basic data types in reverse order by using sort in array, that is, sorting from large to small.
At first, I thought that I had just learned and did not know a better solution. I had been searching for wow on the Internet and found many methods,
It's complicated and messy, but it's not actually the answer to my questions. For this reason, I also asked questions on Baidu,
No one answered this question till now, and I don't know if there is any better solution.
But there is a solution, that is, when you finish sorting in ascending order, you can use a for loop to reverse all the order,
Paste the Code:
Package com; import Java. util. arrays; import Java. util. sort; // ascending, ascending order // descending, descending orderpublic class demosort {public static void print (INT [] A) {for (INT I = 0; I <. length; I ++) {system. out. print (A [I]); if (I =. length-1) {system. out. println ();} else {system. out. print ("") ;}} public static int [] reversearrays (INT [] A) {for (INT I = 0; I <. length/2; I ++) {A [I] = A [. length-1-I];} return a;} public static void main (string [] ARGs) {int N; int [] A; input = new partition (system. in); While (input. hasnext () {n = input. nextint (); A = new int [N]; for (INT I = 0; I <n; I ++) {A [I] = input. nextint ();} print (a); // arrays. sort (A, new democomparator (); // if you use comparator, you have to use the packaging class integer to implement arrays. sort (a); // I really don't want to talk about anything. I don't know how to solve it after reading it for a long time, so I can only sort it in ascending order, then in the for reverse array bar a = reversearrays (a); print (a); system. out. println ();}}}
Then we use integer and comparator. in fact, this method is good. At least we can sort the objects in the future. Some errors have occurred in the middle, leading to some errors,
First-out comparator class:
Package com; import Java. util. comparator; public class democomparator implements comparator <integer> {// note that the comparator interface is completed, rather than inheriting public int compare (integer O1, integer O2) {// a very simple CMP function similar to the return O1-O2 function in C ++ ;}}
Then there is the implementation class:
Package com; import Java. util. imports; import Java. util. arrays; public class demosortbyinteger {public static void main (string [] ARGs) {writable input = new writable (system. in); int N; while (input. hasnext () {n = input. nextint (); integer [] A = new integer [N]; for (INT I = 0; I <n; I ++) {string S = input. next (); // input is used to obtain the string. next (); the string type A [I] = integer. valueof (s); // In fact, many methods in integer are static, so you can use integer directly. Then remember to return an integer object} arrays every time you use it. sort (A, new democomparator (); // use comparator to sort objects. This is really awesome. // If list is used, you have to use the collections container, for (INT I = 0; I <n; I ++) {system. out. println (A [I]); // data of the int type of the string type is stored in the integer class, so you can directly output the data }}}}