Java类的排序(按照某个字段,实现compareable接口)

方式一:实现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);
	}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值