Home > Back-end > Don't understand, ask: sort the list for help
Don't understand, ask: sort the list for help
Time:11-08
If inscribe There is a List Collection People object has the following five attribute Name age professional telephone address
There is such a collection Name age 1 professional phone 1 address 1 Name age 2 telephone address 2 Name 3 age 3 professional 3 Name four professional four phone 4 address 4 Name five age 5 Name 6 age 6 professional address
Demand is carried out in accordance with the number of field has value object sorting Field is worth much less in front of him in the back of the Field has a value number as random row
After sorting is
Name age 1 professional phone 1 address 1 Name age 2 telephone address 2 Name 6 age 6 professional address Name four professional four phone 4 address 4 Name 3 age 3 professional 3 Name five age 5
Would you please tell me what the method or the train of thought? Thank you very much
CodePudding user response:
Rewrite the compareTo method, if can the People object with a said several attribute value has a value of a variable, just according to this judgment, if can't, just within the method, in turn, determine the two attributes of an object, if you put in front of the big, in fact is the same
For (int I=0; i For (int j=0; J & lt; List. The size () - I - 1; J++) { If (checkObjFieldIsNull (list. Get (j)) & gt; CheckObjFieldIsNull (list. Get (j + 1))) { R R=list. Get (j); A list. Set (j, the list. The get (j + 1)); A list. Set (j + 1, r); } } } Public static int checkObjFieldIsNull (Object obj) throws IllegalAccessException { Int num=0; For (Field f: obj. GetClass () getDeclaredFields ()) { F.s etAccessible (true); If (f.g et (obj)==null) { num++; } } Return num. }