Sometimes you need to sort the elements in the logarithm group. Of course, you can write a proper sorting method by yourself, but since the Java package contains the arrays. Sort sorting method
Why not use an array with few elements?
Sorting an array 1. Number sorting int [] intarray = new int [] {4, 1, 3,-23
};
Arrays. Sort (intarray );
Output: [-23, 1, 3, 4]
2. Sort strings in upper case and lower case string [] strarray = new string [] {"Z", "A", "C "};
Arrays. Sort (strarray );
Output: [C, A, Z]
3. Strictly sort by alphabet, that is, ignore case-insensitive sort
Arrays. Sort (strarray, String. case_insensitive_order );
Output: [A, C, Z]
4. Reverse sorting, reverse-Order sort
Arrays. Sort (strarray, collections. reverseorder ());
Output: [Z, a, c]
5. Ignore case-insensitive reverse sorting case-insensitive reverse-Order sort
Arrays. Sort (strarray, String. case_insensitive_order );
Collections. Reverse (arrays. aslist (strarray ));
Output: [Z, C, A]
The most common mistake for Java beginners is to try to write some methods to complete the array sorting function. In fact, the array sorting function has already been implemented in Java APIs. We do not have
We need to repeat the wheel.
The arrays class has a static method sort. With this method, we can import the array we want to sort into the sorting. Because we pass in an array reference, the sorting is complete.
The result also uses this reference to change the array. for sorting integers and strings, JDK provides the default implementation. If you want to sort an array of objects, you must implement
Java. util. comparator interface.
Package COM. gjh. gee. arrays; <br/> Import Java. util. arrays; <br/> public class arraysortdemo {<br/> Public void sortintarray () {<br/> int [] arraytosort = new int [] {48, 5, 89, 80, 81, 23, 45, 16, 2}; <br/> system. out. println ("Before sorting"); <br/> for (INT I = 0; I <arraytosort. length; I ++) <br/> system. out. println (arraytosort [I]); <br/> // call the static sorting method sort of the array <br/> arrays. sort (arraytosort); <br/> system. out. println ("sorted"); <br/> for (INT I = 0; I <arraytosort. length; I ++) <br/> system. out. println (arraytosort [I]); <br/>}< br/> Public void sortstringarray () {<br/> string [] arraytosort = new string [] {"Oscar", "Charlie", "Ryan", <br/> "Adam", "David "}; <br/> system. out. println ("Before sorting"); <br/> for (INT I = 0; I <arraytosort. length; I ++) <br/> system. out. println (arraytosort [I]); <br/> system. out. println ("sorted"); <br/> // call the static sorting method sort of the array <br/> arrays. sort (arraytosort); <br/> for (INT I = 0; I <arraytosort. length; I ++) <br/> system. out. println (arraytosort [I]); <br/>}< br/> Public void sortobjectarray () {<br/> dog O1 = new dog ("dog1", 1 ); <br/> dog O2 = new dog ("dog2", 4); <br/> dog O3 = new dog ("dog3", 5 ); <br/> dog O4 = new dog ("dog4", 2); <br/> dog O5 = new dog ("dog5", 3 ); <br/> dog [] dogs = new dog [] {O1, O2, O3, O4, O5}; <br/> system. out. println ("Before sorting"); <br/> for (INT I = 0; I <dogs. length; I ++) {<br/> dog = dogs [I]; <br/> system. out. println (dog. getname (); <br/>}< br/> arrays. sort (dogs, new byweightcomparator (); <br/> system. out. println ("sorted:"); <br/> for (INT I = 0; I <dogs. length; I ++) {<br/> dog = dogs [I]; <br/> system. out. println (dog. getname (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> arraysortdemo T = new arraysortdemo (); <br/> T. sortintarray (); <br/> T. sortstringarray (); <br/> T. sortobjectarray (); <br/>}< br/>
Package COM. gjh. gee. arrays; <br/> public class dog {<br/> private string name; <br/> private int weight; <br/> Public dog (string name, int weight) {<br/> This. setname (name); <br/> This. weight = weight; <br/>}< br/> Public int getweight () {<br/> return weight; <br/>}< br/> Public void setweight (INT weight) {<br/> This. weight = weight; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/>
Package COM. gjh. gee. arrays; <br/> Import Java. util. comparator; <br/> public class byweightcomparator implements comparator {<br/> Public final int compare (Object pfirst, object metadata Cond) {<br/> int afirstweight = (DOG) pfirst ). getweight (); <br/> int asecondweight = (DOG) implements Cond ). getweight (); <br/> int diff = afirstweight-asecondweight; <br/> If (diff> 0) <br/> return 1; <br/> If (diff <0) <br/> return-1; <br/> else <br/> return 0; <br/>}< br/>}
Next we will add a collection. Sort sorting rule, which is similar to arrays. sort.
// Pojo example <br/> class user {<br/> string name; <br/> string age; </P> <p> public user (string name, string age) {<br/> This. name = Name; <br/> This. age = age; <br/>}< br/> Public String getage () {<br/> return age; <br/>}< br/> Public void setage (string age) {<br/> This. age = age; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}</P> <p> // comparison class to implement the comparator interface <br/> Import Java. util. comparator; <br/> Import Java. util. list; <br/> Import Java. util. arraylist; <br/> Import Java. util. collections; <br/> public class comparatoruser implements comparator {<br/> Public int compare (Object arg0, object arg1) {<br/> User user0 = (User) arg0; <br/> User user1 = (User) arg1; <br/> // compare the age. If the age is the same, compare the name <br/> int flag = user0.getage (). compareto (user1.getage (); <br/> If (flag = 0) {<br/> return user0.getname (). compareto (user1.getname (); <br/>} else {<br/> return flag; <br/>}</P> <p> // test class <br/> public class sorttest {</P> <p> Public static void main (string [] ARGs) {<br/> List userlist = new arraylist (); <br/> userlist. add (new user ("DD", "4"); <br/> userlist. add (new user ("AA", "1"); <br/> userlist. add (new user ("ee", "5"); <br/> userlist. add (new user ("BB", "2"); <br/> userlist. add (new user ("FF", "5"); <br/> userlist. add (new user ("cc", "3"); <br/> userlist. add (new user ("GG", "6"); </P> <p> comparatoruser comparator = new comparatoruser (); <br/> collections. sort (userlist, comparator); </P> <p> for (INT I = 0; I <userlist. size (); I ++) {<br/> User user_temp = (User) userlist. get (I); <br/> system. out. println (user_temp.getage () + "," + user_temp.getname (); <br/>}</P> <p >}< br/>}
// First, sort by age. If the age is the same, sort by name.
Result:
1, AA
2, BB
3, CC
4, dd
5, EE // Note: if the person is also 5 years old, the name (EE, ff) is compared and then sorted.
5, FF
6, GG