The one that was written a few days ago is wrong, here will be the correct update ...
By implementing the comparator interface and rewriting the Compare function, the custom collation implements the ordering of objects in ArrayList.
Student class Definition:
Automatically generate set and get methods by right-clicking-"source-"
package;
Import Java.util.Comparator;
Import java.io.*;
public class Student implements serializable,comparator<student> {
String ID;
String name;
int score;
Public String getId () {
return ID;
}
public void SetId (String id) {
This.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int Getscore () {
return score;
}
public void SetScore (int result) {
This.score = result;
}
public void SetAll (String id,string name,int score) {
This.id=id;
This.name=name;
This.score=score;
}
Public String toString () {
Return (this.id+ "" +this.name+ "" +this.score);
}
@Override
public int Compare (Student stu_1, Student stu_2) {
if (Stu_1.score>stu_2.score)
return-1;
Else
return 1;
}
}
Application:
arraylist<student> Stuarray = new arraylist<student> ();
Collections.sort (Stuarray, New Student ());
This enables the sorting