1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package top.lrshuai.blog.util;
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;
public class Player{ private String userId; private String userName; private int level; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public Player() { super(); } public Player(String userId, String userName, int level) { super(); this.userId = userId; this.userName = userName; this.level = level; } @Override public String toString() { return "Player [userId=" + userId + ", userName=" + userName + ", level=" + level + "]"; } public static void main(String[] args) { List<Player> playerList = new ArrayList<>(); Player p1 = new Player("p1", "abc", 1); Player p2 = new Player("p2", "def", 2); Player p3 = new Player("p3", "efg", 5); Player p4 = new Player("p4", "bcd", 3); playerList.add(p1); playerList.add(p2); playerList.add(p3); playerList.add(p4); Collections.sort(playerList, new Comparator<Player>() { @Override public int compare(Player o1, Player o2) { if (o1.getLevel() < o2.getLevel()) return 1; else if(o1.getLevel() > o2.getLevel()){ return -1; }else{ return o1.getUserName().compareTo(o2.getUserName()); } } });; System.out.println(playerList); } }
|