list内对象的属性的排序

package mianshi;

public class Student {

	int id;
	String name;
	double score;
	int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	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;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
  package mianshi;

import java.util.Comparator;

public class ComparatorStudent implements Comparator<Object> {


	@Override
	public int compare(Object o1, Object o2) {
		Student s1 = (Student) o1;
		Student s2 = (Student) o2;
		int i=0 ;
		if(s1.getAge() == s2.getAge()){
			i= 0;
		}else if(s1.getAge() > s2.getAge()){
			i= 1;
		}else if(s1.getAge() < s2.getAge()){
			i= -1;
		}
		return i ;
	}

}
  package mianshi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class ListSort {

    List list = new ArrayList();
	
	public static void main(String[] args) {
		ListSort listSort = new ListSort();
		listSort.addStudent(1, "one", 100, 23);
		listSort.addStudent(2, "two", 100, 20);
		listSort.addStudent(3, "three", 100, 16);
		listSort.addStudent(4, "four", 100, 24);
		
		ComparatorStudent comparator = new ComparatorStudent();
		Collections.sort(listSort.list, comparator);
		
		
		Iterator<Student> iterator = listSort.list.iterator();
		while(iterator.hasNext()){
			System.out.println(iterator.next().age);
		}
		
	}
	
	public void addStudent(int id,String name,double score,int age){
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		student.setScore(score);
		student.setAge(age);
		list.add(student);
	}
	
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值