Java 自定义对象,按指定的某些属性进行排序

利用Arrays.sort()对对象进行按某些属性排序,两种实现方式,内部比较器(comparable)和外部比较器(comparator)。

1.内外比较器的区别

 同:都是接口类型,实现对象的排序

        compareTo(Object o) : 本类的属性>传入对象属性,返回正数,为升序排列;反之为降序

        compare(Object o1,Object o2):第一个参数对象(o1)的属性 > 第二个参数对象(o2)的属性,为升序排列;反之为降序

 异:1.comparable 在java.lang 包中,而comparator 在java.util包中

         2.实现comparable接口的方法为compareTo(Object o),而实现comparator的方法为  compare(Object o1,Object o2)

         3.Comparator 使用灵活,不需要修改源码.但是,使用时需要传入比较器对象;Comparable使用简单,但是需要修改源码

2.实现案例

 分别利用内部、外部比较器对Score、Stu类的对象进行排序

import java.util.Arrays;
import java.util.Comparator;

/**
 * 按指定对象的某个属性排序
 * @author lyf3312
 *
 */
public class ObjectSort {

	//测试排序结果
	public static void main(String[] args) {
/*
		//内部比较器
		Score s1 = new Score(90, 80, 110);
		Score s2 = new Score(91, 90, 98);
		Score s3 = new Score(90, 88, 80);
		Score s4 = new Score(97, 86, 100);
		Score[] s = {s1,s2,s3,s4};
//		Arrays.sort(s); //语文成绩升序排列
		Arrays.sort(s); //语文成绩升序排列,语文成绩相同,按数学成绩降序排列
		for(Score ss :s) {
			ss.show();
		}
		
 */		//外部比较器
		 
		Stu stu1 = new Stu("lee", 20, 70.22);
		Stu stu2 = new Stu("ggg", 20, 50.22);
		Stu stu3 = new Stu("json", 28, 60.69);
		Stu stu4 = new Stu("python", 32, 88.66);
		Stu[]  stu = {stu1,stu2,stu3,stu4};
//		Arrays.sort(stu,stu1); // 按年龄升序排序
		Arrays.sort(stu,new StuCompare()); // 按年龄升序排序,年龄相同按体重升序
		for(Stu st : stu) {
			st.show();
		}
	}
}

/**
 * 待排序对象,利用内部比较器进行排序
 * @author Administrator
 *
 */
class Score implements Comparable<Score>{
	
	public int math;
	public int chinese;
	public int english;
	public Score(int chinese, int math , int english) {
		super();
		this.math = math;
		this.chinese = chinese;
		this.english = english;
	}

	public Score() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public void show() {
		System.out.print("chinese:"+chinese+"\tmath"+math+"\tenglish:"+english+"\n");
	}

	//按单一属性排列
	/*
	@Override
	public int compareTo(Score o) {
		// TODO Auto-generated method stub
		if (this.chinese > o.chinese) {
			return 1;
		}else if(this.chinese < o.chinese) {
			return -1;
		}else {
			return 0;
		}
	}
	*/

	//按多个属性排列
	@Override
	public int compareTo(Score o) {
		// TODO Auto-generated method stub
		if (this.chinese > o.chinese) {
			return 1;
		}else if(this.chinese < o.chinese) {
			return -1;
		}else {
			//语文成绩相同,按数学成绩降序排列
			if(this.math >o.math) {
				return -1;
			}else if(this.math < o.math) {
				return 1;
			}else {
				return 0;
			}
		}
	}
	
}
/**
 * 待排序对象,利用外部比较器进行排序
 * @author Administrator
 *
 */
class Stu{
	public String name;
	public int age;
	public double weight;
	public Stu(String name, int age, double weight) {
		super();
		this.name = name;
		this.age = age;
		this.weight = weight;
	}
	public Stu() {
		super();
		// TODO Auto-generated constructor stub
	}
	public void show() {
		System.out.println("name:"+name+"\tage:"+age+"\tweight:"+weight+"\n");
	}
	
}
//stu的外部比较器
class StuCompare implements Comparator<Stu>{

	//按单一属性排序
		/*
		@Override
		public int compare(Stu o1, Stu o2) {
			// TODO Auto-generated method stub
			if(o1.age > o2.age) {
				return 1;
			}else if(o1.age < o2.age) {
				return -1;
			}else {
				return 0;
			}
		}
		*/
	//按多个属性排序
		@Override
		public int compare(Stu o1, Stu o2) {
			// TODO Auto-generated method stub
			if(o1.age > o2.age) {
				return 1;
			}else if(o1.age < o2.age) {
				return -1;
			}else {
				if(o1.weight > o2.weight) {
					return 1;
				}else if(o1.weight < o2.weight) {
					return -1;
				}else {
					return 0;
				}
			}
		}
	
}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用Comparator接口来按照对象的多个属性进行排序。 首先,我们需要定义一个实现了Comparator接口的自定义比较器类。在该类中,我们可以重写compare方法,该方法用于比较两个对象的多个属性。 比如,假设我们有一个Person类,其中包含了name、age和salary三个属性。现在我们想要按照name属性进行排序,如果name相同的话,则按照age属性进行排序,最后再按照salary属性排序。 首先,我们定义一个PersonComparator类来实现Comparator接口: ``` public class PersonComparator implements Comparator<Person> { @Override public int compare(Person p1, Person p2) { int result = p1.getName().compareTo(p2.getName()); if (result == 0) { result = p1.getAge() - p2.getAge(); if (result == 0) { result = p1.getSalary() - p2.getSalary(); } } return result; } } ``` 接下来,我们可以使用该比较器来对Person对象进行排序。比如,我们创建了一个Person列表,其中包含了多个Person对象,然后调用Collections类的sort方法进行排序: ``` List<Person> personList = new ArrayList<>(); // 添加Person对象到列表中 Collections.sort(personList, new PersonComparator()); ``` 这样,就可以按照name、age和salary属性顺序对Person对象进行排序了。 需要注意的是,在compare方法中,我们可以根据需要自定义比较的逻辑,比如可以使用compareTo方法来比较字符串属性,或者使用减法来比较数值属性。最后,将比较的结果返回即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值