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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章