Use collections. the sort method can sort the list in two ways. The first method is to implement the comparable interface for objects in the list, as follows:/*** sort users by Order */Public classuser implements comparable {privatestring name; privateinteger order; publicstring getname () {returnname;} publicvoid setname (string name) {This. name = Name;} publicinteger getorder () {returnorder;} publicvoid setorder (integer order) {This. order = order;} publicint compareto (User arg0) {returnthi S. getorder (). compareto (arg0.getorder () ;}} test: Public classtest {publicstatic void main (string [] ARGs) {user user1 = new user (); user1.setname (""); user1.setorder (1); User user2 = new user (); user2.setname ("B"); user2.setorder (2); list = new arraylist (); // Add user2 and then add user1 list. add (user2); list. add (user1); collections. sort (list); For (User U: List) {system. out. println (U. getname () ;}} enter The result is as follows AB. The second method is based on collections. the sort overload method is implemented. For example,/*** sorts users by Order */Public classuser {// The comparable interface privatestring name; privateinteger order; publicstring getname () is not required here () {returnname;} publicvoid setname (string name) {This. name = Name;} publicinteger getorder () {returnorder;} publicvoid setorder (integer order) {This. order = order ;}} in the main class: Public classtest {publicstatic void main (string [] ar GS) {user user1 = new user (); user1.setname ("A"); user1.setorder (1); User user2 = new user (); user2.setname ("B "); user2.setorder (2); list = new arraylist (); list. add (user2); list. add (user1); collections. sort (list, newcomparator () {publicint compare (User arg0, user arg1) {returnarg0.getorder (). compareto (arg1.getorder () ;}}); For (User U: List) {system. out. println (U. getname () ;}} the output result is as follows: The structure is simple, but it can only be sorted by fixed attributes. The latter is flexible. You can temporarily specify the sorting items, but the code is not concise enough for multiple fields: view source code printing? Collections. sort (list, newcomparator () {publicint compare (User arg0, user arg1) {// The first professional Inti = arg0.getorder (). compareto (arg1.getorder (); // if the major is the same, perform the second comparison if (I = 0) {// The second comparison intj = arg0.getxxx (). compareto (arg1.getxxx (); // if the educational system is the same, if (j = 0) {returnarg0.getccc () is returned by age (). compareto (arg1.getccc () ;}returnj ;}returni ;}});