方式一:实现comparable接口
User.java
package objectComparable;
public class User implements Comparable{
private Integer id;
private String username;
private Integer score;
private String address;
public User(Integer id, String username, Integer score) {
super();
this.id = id;
this.username = username;
this.score = score;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + score + ", address=" + address + "]";
}
@Override
public int compareTo(Object o) {
User user=(User) o;
if(this.score>user.score){
return 1;
}
else if(this.score<user.score){
return -1;
}else{
if(this.id>user.id){
return 1;
}
else if(this.id<user.id){
return -1;
}else{
return 0;
}
}
}
}
UserTest.java
package objectComparable;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class UserTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
List<User> list=new ArrayList();
list.add(new User(1, "夏末", 12));
list.add(new User(2, "夏末2", 12));
list.add(new User(2, "夏末", 13));
list.add(new User(4, "夏末", 12));
list.add(new User(0, "夏末", 12));
Collections.sort(list);
for(User user : list){
System.out.println(user);
}
}
}
方式二:使用匿名内部类实现
Person.java
package objectComparable;
public class Person {
private Integer id;
private String name;
private Integer score;
public Person(Integer id, String name, Integer score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
@Override
public String toString() {
return “Person [id=” + id + “, name=” + name + “, score=” + score + “]”;
}
}
PersonTest.java
package objectComparable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class PersonTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
List<Person> listp=new ArrayList<Person>();
listp.add(new Person(1, "夏末", 1));
listp.add(new Person(3, "夏末", 102));
listp.add(new Person(2, "夏末", 120));
listp.add(new Person(4, "夏末", 2));
listp.add(new Person(0, "夏末", 0));
Collections.sort(listp, new Comparator<Person>() {
public int compare(Person o1, Person o2) {
if(o1.getScore()>o2.getScore()){
return 1;
}else if(o1.getScore()<o2.getScore()){
return -1;
}else{
return 0;
}
}
});
for (Person person : listp) {
System.out.println(person);
}
}
}