Home > Back-end >  Beginners to the LIST from the reorder
Beginners to the LIST from the reorder

Time:01-22

Such as:
List List=new ArrayList (a);
List. The add (new User (" A ", 1));
List. The add (new User (" B ", 1));
List. The add (new User (" C ", 1));
List. The add (new User (" A ", 2));
List. The add (new User (" B ", 2));
List. The add (new User (" B ", 4));
List. The add (new User (" A ", 3));
List. The add (new User (" A ", 4));
List. The add (new User (" A ", 7));
List. The add (new User (" B ", 7));
List. The add (new User (" C ", 4));

Want to output:
Name: A, age: 1
Name: A, age: 2
Name: A, age: 3
Name: A, age: 4
Name: B, age: 1
Name: B, age: 2
Name: B, age: 4
Name: C, age: 1
Name: A, age: 7
Name: B, age: 7
Name: C, age: 4


Note: in the original list order, reorder, according to the same first name, and then according to the age difference within 2 (last) compared with the next, jdk7

CodePudding user response:

Collections. The sort (list) method a try, I remember this and object to rewrite a way

CodePudding user response:

https://blog.csdn.net/qq_40298231/article/details/109646886, may refer to this blog, realize the entity class attributes of the object collation

CodePudding user response:

reference 1st floor liushuo9999 response:
Collections. The sort (list) method a try, I remember this and object to rewrite a way


Tried

 

The Collections. The sort (list, new Comparator () {
@ Override
Public int the compare (User u1, the User u2) {
Int the diff=u1. GetAge () - u2. GetAge ();
If (diff & gt;=- 2 & amp; & The diff & lt;=2) {
return 1;
}
return 0;
}
});


So only sort of age, and have the same name

CodePudding user response:

You the ordering rules in a sense, very troublesome
By the same name, and then according to the age difference within 2
<- sorting steps - & gt;
A rules are written in Chinese and for A4 and A7 difference among 3 so not discharge A7
A1, A2, A3, A4
Another row B
A1, A2, A3, A4, B1, B2, B4, because among B4 and B7 sent 3 so not row of B7
Another row C
A1, A2, A3, A4, B1, B2, B4, C1, because among C1 and C4 sent 3 so not row of C4
<- sorting steps - & gt;
Sort according to the above steps to row again A7, B7, C4
This goes to recursion, because these parameters are not fixed, if there are other parameters must be recursive, such as much A11, B11, C7, etc.

CodePudding user response:

Actually not difficult also
First step grouped by name, according to the age in each group, deposited in the map (key: the name, value: list -> According to the age)
The second step is to map the key order
The third step, according to the map key sequence cycle, first take the first key value list Data, insert a new sort the result list, until the age difference is greater than 2 to remove a key value list Data into the list until age 2 and then remove the value of a key list Data, and so forth, the key to the last it returns the first key, so repeated cycle, until the value of the map list Have been remove out, sort the result list data is sorted results

CodePudding user response:

According to the thinking of 5 l, give you write a piece of sample code

 public class Sample {
Static class User {
String name;
Int the age;
Public User (String name, int age) {enclosing name=name; Enclosing the age=age; }
}
Public static void main (String [] args) {
Try {
List List=new ArrayList (a);
List. The add (new User (" A ", 1));
List. The add (new User (" B ", 1));
List. The add (new User (" C ", 1));
List. The add (new User (" A ", 2));
List. The add (new User (" B ", 2));
List. The add (new User (" B ", 4));
List. The add (new User (" A ", 3));
List. The add (new User (" A ", 4));
List. The add (new User (" A ", 7));
List. The add (new User (" B ", 7));
List. The add (new User (" C ", 4));

Comparator KeyComp=new Comparator () {//name comparator
Public int the compare (String s1, String s2) {
Return to s1.com pareTo (s2);
}
};
Comparator UserComp=new Comparator () {//age comparator
Public int the compare (User u1, the User u2) {
Return u1. Age - u2. Age;
}
};

Map The map=new HashMap<> (a);//grouped by the name
for (int i=0; IThe User u=list. Get (I);
if (! Map. Either containsKey (u.n ame)) {
The map. The put (u.n ame, new ArrayList<> ());
}
Map. Get (u.n ame). The add (u);
}
List Keys=new ArrayList<> (map) keySet ());
Keys. Sort (keyComp);//the map key sort by name
for (int i=0; IMap. Get (keys. Get (I)), sort (userComp);//the value of the map list sort by age
}
List Result=new ArrayList<> (a);//sort the results
While (true) {
for (int i=0; IList The users=map. Get (keys. Get (I));
While (users. The size () & gt; 0) {//key corresponding to the value of the list data circulation processing is
If (result. The size () & gt; 0 {
The User last=result. The get (result. The size () - 1);//on a data
The User cur=users. Get (0);//the current data
If (last) name) equals (cur. Name) & amp; & The last. The age + 2 & lt; Cur. Age) {//until the current sort data does not meet the conditions for
break;
}
}
Result. The add (users. Remove (0));//or removed from the list of value data is inserted into the sorting result in
}
}
If (result. The size ()==list. The size ()) break;//quantity of sorting results are available and the same number before then end
}

for (int i=0; ISystem. Out. Printf (" name: % s, age: % d \ n ", the result, the get (I). The name, the result, the get (I). The age);
}
} the catch (Throwable e) {
e.printStackTrace();
}
}
}
  • Related