Java object that implements the List to sort
According to the object of a property of the object List sorted.
Four Heroes to performance ranking, for example, to sort the poets.
Java implementation as follows:
1, the poet (Poet) structure class, defined as follows:
/** * Created by Miracle Luna on 2020/1/11 */ public class Poet { private String name; private Double score; public Poet(String name, Double score) { this.name = name; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } @Override public String toString() { return "Poet{" + "name='" + name + '\'' + ", score=" + score + '}'; } }
2, according to the poet standings, as follows:
import java.util.ArrayList; import java.util.Comparator; import java.util.List; /** * Created by Miracle Luna on 2020/1/11 */ public class PoetSort { public static void main(String[] args) { List<Poet> poetList = new ArrayList<Poet>(); Poet poet1 = new Poet("杨炯", 94.0); poetList.add(poet1); Poet poet2 = new Poet("卢照邻", 92.5); poetList.add(poet2); Poet poet3 = new new Poet ( "LUO", 95.0 ); poetList.add (poet3); Poet poet4 = new new Poet ( "Wang Bo", 99.5 ); poetList.add (poet4); // original order System.out.println ( "== > initial sequence is as follows: " ); poetList.forEach (the na me -> System.out.println (poet.toString ())); // accordance ranking scores (Low) poetList.sort ( new new Comparator <Poet> ( ) { @Override public int Compare (Poet poet1, Poet poet2) { Double score1 = poet1.getScore (); Double score2= Poet2.getScore (); return score2 .compareTo ( score1 ); } }); System.out.println ( "\ n-==> ranking scores in accordance with ( high to low ) as follows:" ); poetList.forEach (the na me -> System.out.println (poet.toString ())); // accordance ranking scores (from low to high) poetList.sort ( new new Comparator <Poet> () { @Override public int Compare (Poet poet1, Poet poet2 ) { Double score1 = poet1.getScore (); Double score2 =poet2.getScore (); return score1 .compareTo ( score2 ); } }); System.out.println ( "\ n-==> ranking scores in accordance with ( from low to high ) as follows:" ); poetList.forEach (the na me - > System.out.println (poet.toString ())); } }
3. The results are as follows:
==> Initial sequence is as follows: Poet {name = 'Yang Jiong', Score = 94.0 } Poet {name = 'Lu Zhaolin', Score = 92.5 } Poet {name = 'LUO', Score = 95.0 } Poet {name = 'WangBo ', score = 99.5 } ==> according to the ranking score ( highest to lowest ) is as follows: Poet {name =' Wang Bo ', score = 99.5 } Poet {name =' LUO ', score = 95.0 } Poet {name =' Yang Gordon ', score = 94.0 } Poet {name =' Lu Zhaolin ', score = 92.5 } ==> according to ranking scores ( from low to high ) as follows: Poet {name =' Lu Zhaolin ', score = 92.5 } Poet {name ='Yang Jiong ', score = 94.0Yang Jiong ', Score = 94.0 } {Name Poet = 'LUO', Score = 95.0 } Poet {name = 'Wang Bo', score = 99.5}