用java语言对编号进行排序_Java中对对象进行排序-Go语言中文社区

1,首先创建一个类class Person3{

private String name;

private int age;

private double score;

public Person3(String name, int age, double score) {

super();

this.name = name;

this.age = age;

this.score = score;

}

@Override

public String toString() {

return "Person3 [name=" + name + ", age=" + age + ", score=" + score

+ "]";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getScore() {

return score;

}

public void setScore(double score) {

this.score = score;

}

}

这个时候再产生这个类的对象,将对象存入数组里,并对这个数组进行排序public class Test17 {

public static void main(String[] args) {

Person3[] array = new Person3[3];

array[0] = new Person3("caocao",18,29);

array[1] = new Person3("liubei",20,49);

array[2] = new Person3("guanyu",21,77);

//对这个数组排序

Arrays.sort(array);

System.out.println(Arrays.toString(array));

}

}

我们会发现运行结果出现了异常

3ebe85c083803b43a01d8a587fa5df33.png

因为你并不知道应该是依照这个对象的什么属性进行排序

因此我们需要去重写一个compareTo方法,重写这个方法的时候应该要继承一个接口Comparable。

查看这个接口的源码public interface Comparable {

/**

* Compares this object with the specified object for order. Returns a

* negative integer, zero, or a positive integer as this object is less

* than, equal to, or greater than the specified object.

*

*

The implementor must ensure sgn(x.compareTo(y)) ==

* -sgn(y.compareTo(x)) for all x and y. (This

* implies that x.compareTo(y) must throw an exception iff

* y.compareTo(x) throws an exception.)

*

*

The implementor must also ensure that the relation is transitive:

* (x.compareTo(y)>0 && y.compareTo(z)>0) implies

* x.compareTo(z)>0.

*

*

Finally, the implementor must ensure that x.compareTo(y)==0

* implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for

* all z.

*

*

It is strongly recommended, but not strictly required that

* (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any

* class that implements the Comparable interface and violates

* this condition should clearly indicate this fact. The recommended

* language is "Note: this class has a natural ordering that is

* inconsistent with equals."

*

*

In the foregoing description, the notation

* sgn(expression) designates the mathematical

* signum function, which is defined to return one of -1,

* 0, or 1 according to whether the value of

* expression is negative, zero or positive.

*

* @param o the object to be compared.

* @return a negative integer, zero, or a positive integer as this object

* is less than, equal to, or greater than the specified object.

*

* @throws NullPointerException if the specified object is null

* @throws ClassCastException if the specified object's type prevents it

* from being compared to this object.

*/

public int compareTo(T o);

}

接下来我们来重写这个方法,并使通过年龄来排序class Person3 implements Comparable{

private String name;

private int age;

private double score;

public Person3(String name, int age, double score) {

super();

this.name = name;

this.age = age;

this.score = score;

}

@Override

public String toString() {

return "Person3 [name=" + name + ", age=" + age + ", score=" + score

+ "]";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public double getScore() {

return score;

}

public void setScore(double score) {

this.score = score;

}

@Override

public int compareTo(Person3 o) {

// TODO Auto-generated method stub

return age-o.age;

}

}

57b52c618dc7754971f54fd4e6f0bffd.png

分别通过姓名和成绩

18279bf6b5012c7e986bb912639b1d16.png

046a197e0aab9572a1b866b78c000f50.png

但是这样写的话我们会发现每次想要改一下比较什么比较麻烦

于是我们改用

dc02f297cdc157c9c2c7cc38c2b1a853.png这个方法来实现Arrays.sort(array, new Comparator(){

//传入的时候由用户自己选

@Override

public int compare(Person3 o1, Person3 o2) {

// TODO Auto-generated method stub

int age1 = o1.getAge();

int age2 = o2.getAge();

return age1 > age2 ? age1:(age1==age2) ? 0:-1;

}

});Comparator是一个接口,这个是使用这个接口实现了一个匿名内部类,在里面重写compare方法,来用来比较这个对象

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值