java的比较,如何通过整数排序? [英] java comparator, how to sort by integer?

查看:137
本文介绍了java的比较,如何通过整数排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去了解比较在Java中,我发现这个伟大的例子在网上,我的问题是如何将这种code被改变,这样的宠物的名字是由年龄和降序排序,使最古老的是第一而最年轻的是最后一个?

 类狗实现比较<狗>中可比<狗> {
私人字符串名称;
私人诠释年龄;
狗(){
}

狗(串N,诠释一){
  NAME = N;
  年龄=一个;
}

公共字符串getDogName(){
  返回名称;
}

公众诠释getDogAge(){
  返回年龄;
}

//重写compareTo方法
公众诠释的compareTo(犬D){
  返程(this.name).compareTo(d.name);
}

//覆盖比较法的年龄排序
公众诠释比较(狗研发,狗D1){
  返回d.age  -  d1.age;
}
}

公共类实例{
公共静态无效的主要(字符串的args []){
  //获取一个列表Ø狗对象
  名单<狗>名单=新的ArrayList<狗>();

  list.add(新狗(长毛,3));
  list.add(新狗(拉齐,2));
  list.add(新狗(罗杰,10));
  list.add(新狗(汤米,4));
  list.add(新狗(塔米,1));
  Collections.sort(名单); //排序数组列表

  对于(狗狗:列表)//打印名称的排序列表
     System.out.print(a.getDogName()+,);

  //使用排序比较数组列表
  Collections.sort(列表中,新的狗());
  的System.out.println();
  对于(狗狗:列表)//打印时代的排序列表
     System.out.print(a.getDogName()+:+
     a.getDogAge()+,);
}
}
 

解决方案

只要改变

 公众诠释比较(狗研发,狗D1){
  返回d.age  -  d1.age;
}
 

 公众诠释比较(狗研发,狗D1){
  返回d1.age  -  d.age;
}
 

应该对它们进行排序的年龄相反的顺序,如果这是你所期待的。

更新:

@Arian就在他的评论中,宣布一个比较的狗被接受的方式之一是,你声明它作为类本身就是一个公共静态最终字段。

  Dog类实现可比<狗> {
    私人字符串名称;
    私人诠释年龄;

    公共静态最终比较<狗> DESCENDING_COMPARATOR =新的比较<狗>(){
        //覆盖比较法的年龄排序
        公众诠释比较(狗研发,狗D1){
            返回d.age  -  d1.age;
        }
    };

    狗(串N,诠释一){
        NAME = N;
        年龄=一个;
    }

    公共字符串getDogName(){
        返回名称;
    }

    公众诠释getDogAge(){
        返回年龄;
    }

    //重写compareTo方法
    公众诠释的compareTo(犬D){
        返程(this.name).compareTo(d.name);
    }

}
 

您可以再使用它任何地方在code,你想比较狗如下:

  //使用排序比较数组列表
Collections.sort(列表,Dog.DESCENDING_COMPARATOR);
 

另一个重要的事情要记住,当执行比较的是,这是非常重要的的compareTo与equals一致执行。虽然不是必需的,不这样做可能会导致对一些集合奇怪的行为如集的一些实施。对实施的compareTo的发声原理的更多信息,请参见的帖子。

Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last?

class Dog implements Comparator<Dog>, Comparable<Dog>{
private String name;
private int age;
Dog(){
}

Dog(String n, int a){
  name = n;
  age = a;
}

public String getDogName(){
  return name;
}

public int getDogAge(){
  return age;
}

// Overriding the compareTo method
public int compareTo(Dog d){
  return (this.name).compareTo(d.name);
}

// Overriding the compare method to sort the age 
public int compare(Dog d, Dog d1){
  return d.age - d1.age;
}
}

public class Example{
public static void main(String args[]){
  // Takes a list o Dog objects
  List<Dog> list = new ArrayList<Dog>();

  list.add(new Dog("Shaggy",3));
  list.add(new Dog("Lacy",2));
  list.add(new Dog("Roger",10));
  list.add(new Dog("Tommy",4));
  list.add(new Dog("Tammy",1));
  Collections.sort(list);// Sorts the array list

  for(Dog a: list)//printing the sorted list of names
     System.out.print(a.getDogName() + ", ");

  // Sorts the array list using comparator
  Collections.sort(list, new Dog());
  System.out.println(" ");
  for(Dog a: list)//printing the sorted list of ages
     System.out.print(a.getDogName() +"  : "+
     a.getDogAge() + ", ");
}
}

解决方案

Simply changing

public int compare(Dog d, Dog d1) {
  return d.age - d1.age;
}

to

public int compare(Dog d, Dog d1) {
  return d1.age - d.age;
}

should sort them in the reverse order of age if that is what you are looking for.

Update:

@Arian is right in his comments, one of the accepted ways of declaring a comparator for a dog would be where you declare it as a public static final field in the class itself.

class Dog implements Comparable<Dog> {
    private String name;
    private int age;

    public static final Comparator<Dog> DESCENDING_COMPARATOR = new Comparator<Dog>() {
        // Overriding the compare method to sort the age
        public int compare(Dog d, Dog d1) {
            return d.age - d1.age;
        }
    };

    Dog(String n, int a) {
        name = n;
        age = a;
    }

    public String getDogName() {
        return name;
    }

    public int getDogAge() {
        return age;
    }

    // Overriding the compareTo method
    public int compareTo(Dog d) {
        return (this.name).compareTo(d.name);
    }

}

You could then use it any where in your code where you would like to compare dogs as follows:

// Sorts the array list using comparator
Collections.sort(list, Dog.DESCENDING_COMPARATOR);

Another important thing to remember when implementing Comparable is that it is important that compareTo performs consistently with equals. Although it is not required, failing to do so could result in strange behaviour on some collections such as some implementations of Sets. See this post for more information on sound principles of implementing compareTo.

这篇关于java的比较,如何通过整数排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆