自定义对象排序
Arrays 位置 java.util包,
其中.sort 对指定数据进行升序的方式排序
方法一:对自定义对象 必需 要实现 Comparable 接口 并且重写里面 compareTo 方法
import java.util.Arrays;
/**
* 使用arrays对指定数据进行升序排序
* @author Administrator,,这里是调用处
*/
public class Zidingyi_Paixu {
public static void main(String[] args) {
Sort s1 = new Sort(18);
Sort s2 = new Sort(20);
Sort s3 = new Sort(25);
Sort s4 = new Sort(19);
Sort[] s5 = new Sort[]{s1, s2, s3, s4};
//对数组进行升序排序,当然可以是引用类型的数组,
//必须要结合toString和comperable接口,,尤其是实现compareTo,
//才能进行排序不然结果是错误
Arrays.sort(s5);//执行排序
for (Sort sort : s5) {
System.out.println(sort);
}
}
}
public class Sort implements Comparable<Sort>{
private int age;
public Sort(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//该方法进行对象的比价,根据不同的结果返回不同的值,分别为>0, =0, <0;
/**
* 比较方法大概是这么个意思;
* 当返回的值>0该值向后靠,接在又和后面的比较,>0向后靠,当小于时则不交换
* 第一个排完后接着是后面的数进行排序
*/
@Override
public int compareTo(Sort o) {
return this.age - o.age;
}
@Override
public String toString() {
return this.age +"";//将数字强制转换为字符串
}
}
方法二:对自定义对象建立不同的策略,也就建立不同策略对象 ,策略需要实现 Comparator 接口 重写 compare 的方法
import java.util.Arrays;
/**
* 使用策略模式进行多种样式的排序
* 比如人有多种属性,年龄,体重,升高等,使用策略模式能进行多种样式的排序方法
* @author Administrator
*/
//该类表示 人 用来存放各种属性,
public class CelveModel_Duoyang_Paixu {
private int age;
private int weight;
private int high;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHigh() {
return high;
}
public void setHigh(int high) {
this.high = high;
}
//在构造函数中给予三个属性初始值
public CelveModel_Duoyang_Paixu(int age, int weight, int high) {
this.age = age;
this.weight = weight;
this.high = high;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "年龄 " + this.getAge() + " 体重:" + this.getWeight()
+ " 身高 " + this.getHigh();
}
public static void main(String[] args) {
CelveModel_Duoyang_Paixu s1 = new CelveModel_Duoyang_Paixu(18, 100, 175);
CelveModel_Duoyang_Paixu s2 = new CelveModel_Duoyang_Paixu(20, 120, 173);
CelveModel_Duoyang_Paixu s3 = new CelveModel_Duoyang_Paixu(25, 98, 169);
CelveModel_Duoyang_Paixu s4 = new CelveModel_Duoyang_Paixu(19, 140, 178);
CelveModel_Duoyang_Paixu[] s5 = new CelveModel_Duoyang_Paixu[]{s1, s2, s3, s4};
//Arrays.sort 在于comparator联系时,要传入两个参数,一个是待排数组,另一个是
//实际写有compare的类
Arrays.sort(s5, new Sort_Age());//传入两个参数
for (CelveModel_Duoyang_Paixu sort : s5) {
System.out.println(sort);
}
Arrays.sort(s5, new Sort_Weight());//传入两个参数
for (CelveModel_Duoyang_Paixu sort : s5) {
System.out.println(sort);
}
}
}
import java.util.Comparator;
/**
* 根据体重进行排序
* @author Administrator
*/
class Sort_Weight implements Comparator<CelveModel_Duoyang_Paixu>{
@Override
public int compare(CelveModel_Duoyang_Paixu o1, CelveModel_Duoyang_Paixu o2) {
return o1.getWeight() - o2.getWeight();
}
}
import java.util.Comparator;
/**
* 根据年龄来进行排序
* @author Administrator
*/
public class Sort_Age implements Comparator<CelveModel_Duoyang_Paixu>{
//通过Comparator函数传入Celve Model 的某一参数,compare 不同于compareTo
@Override
public int compare(CelveModel_Duoyang_Paixu o1, CelveModel_Duoyang_Paixu o2) {
return o1.getAge() - o2.getAge();
}
}