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

CodePudding user response:

T.R (age=1, career=null, phone=null, address=1)
T.R (age=2, career=null, phone=null, address=2)
T.R (age=3, career=3, phone=null, address=3)
T.R (age=null, career=null, phone=null, address=4)
===================
T.R (age=3, career=3, phone=null, address=3)
T.R (age=1, career=null, phone=null, address=1)
T.R (age=2, career=null, phone=null, address=2)
T.R (age=null, career=null, phone=null, address=4)

 
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.
}

CodePudding user response:

refer to the second floor rain shinohara foam response:
T.R (age=1, career=null, phone=null, address=1)
T.R (age=2, career=null, phone=null, address=2)
T.R (age=3, career=3, phone=null, address=3)
T.R (age=null, career=null, phone=null, address=4)
===================
T.R (age=3, career=3, phone=null, address=3)
T.R (age=1, career=null, phone=null, address=1)
T.R (age=2, career=null, phone=null, address=2)
T.R (age=null, career=null, phone=null, address=4)

 
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.
}

Import the Java. Lang. Reflect. Field;

CodePudding user response:

Add an attribute to people count, the statistics value field here, and then sort for this field

CodePudding user response:

The positive solution on the second floor, rewrite the compareTo method is more convenient

CodePudding user response:

Entity class write a method to calculate the number of attributes is not empty line
@ Data
Public class People {
private String name;
private Integer age;
Private Integer height;

Public People (String name, Integer age, Integer height) {
this.name=name;
this.age=age;
This. Height=height;
}


Private int getCharacter () {
Return (int) Arrays. Stream (enclosing getClass () getDeclaredFields ())
The filter (a - & gt; {
Try {
The return of al-qeada et (this)!=null;
} the catch (IllegalAccessException e) {
}
return false;
}
). The count ();
}

Public static void main (String [] args) {

People p1=new People (" flower ", null, null);
People p2=new People (" bill ", 22175);
People p3=new People (" zhang ", 21, null);
List List=Lists. NewArrayList (p1, p2, p3);
List. Sort (Comparator.com paring (People: : getCharacter));
List. The forEach (System. Out: : println);
}
}
  • Related