Sort list using different keys

The extreme newbie here is struggling to get a handle to all the different ways to sort the list. Suppose I have a list of objects, and each object has several keys that can be used for sorting in different circumstances. I got very useful information from this topic: Comparing Static Comparisons of the Java Interface Comparator using the first Qwerkys example as a generated model:

class Dog {
   private String name;
   private int age;
   private int height;
   private int weight;

   Dog(String n, int a, int b, int c){
      name = n;
      age = a;
      height = b;
      weight = c;
   }
   public String getDogName(){
      return name;
   }
   public int getDogAge(){
      return age;
   } 
   public int getDogHeight(){
      return height;
   }
   public int getDogWeight(){
      return weight;
   }
}

class DogComparator1 implements Comparator<Dog> {
   @Override
   public int compare(Dog d, Dog d1){
      return d.getDogAge() - d1.getDogAge();
   }
}
   class DogComparator2 implements Comparator<Dog> {
   @Override
   public int compare(Dog d, Dog d1){
      return d.getDogHeight() - d1.getDogHeight();
   }
}
   class DogComparator3 implements Comparator<Dog> { 
   @Override
   public int compare(Dog d, Dog d1){
      return d.getDogWeight() - d1.getDogWeight();
   }
}


public class Example{

   public static void main(String args[]){
      // Creat list of dog objects
      List<Dog> dogList = new ArrayList<>();

      // Add a buch of dogs to the list here
               .
               .

      // Create the Comparators
      DogComparator1 compare1 = new DogComparator1();
      DogComparator2 compare2 = new DogComparator2();
      DogComparator3 compare3 = new DogComparator3();


      // Sort the list using Comparators
      Collections.sort(list, compare1);  // Sort by age     
      Collections.sort(list, compare2);  // Sort by height
      Collections.sort(list, compare3);  // Sort by weight          
   }
}

But that just doesn't seem right. I think I want to infer the definitions of the comparator "inside" the Dog class in order to encapsulate them. But, I can’t understand how to do this.

Am I on the right track? If so, then it will be very useful for you to help with the correct syntax.

+4
3

.

Dog:

public static final Comparator<Dog> COMPARE_BY_AGE = new Comparator<Dog>() {
    @Override
    public int compare(Dog d, Dog d1) {
        return d.getDogAge() - d1.getDogAge();
    }
};

public static final Comparator<Dog> COMPARE_BY_HEIGHT = new Comparator<Dog>() {
    @Override
    public int compare(Dog d, Dog d1) {
        return d.getDogHeight() - d1.getDogHeight();
    }
};

public static final Comparator<Dog> COMPARE_BY_WEIGHT = new Comparator<Dog>() {
    @Override
    public int compare(Dog d, Dog d1) {
        return d.getDogWeight() - d1.getDogWeight();
    }
};

:

    // Sort the list using Comparators
    Collections.sort(list, Dog.COMPARE_BY_AGE);  // Sort by age
    Collections.sort(list, Dog.COMPARE_BY_HEIGHT);  // Sort by height
    Collections.sort(list, Dog.COMPARE_BY_WEIGHT);  // Sort by weight
+5

, Dog , . , "" ( ).

.. Dog :

public class Dog {

(...)

    public Comparator<Dog> getAgeComparator() {
        return new AgeComparator();
    }

    private static class AgeComparator implements Comparator<Dog> {
       @Override
       public int compare(Dog d, Dog d1){
          return d.getDogAge() - d1.getDogAge();
       }
}

getAgeComparator() :

public static final Comparator<Dog> AGE_COMPARATOR = new AgeComparator();

. , , . , . " ", . , , - .

, Dog Comparable<Dog>:

public class Dog implements Comparable<Dog> {

    public int compareTo(Dog dog) {
        return AGE_COMPARATOR.compare(this, dog); // or inline comparison here
    }

    (...)
}
+3

Comparator#comparingInt :

  list.sort(Comparator.comparingInt(Dog::getDogAge));  // Sort by age     
  list.sort(Comparator.comparingInt(Dog::getDogHeight));  // Sort by height
  list.sort(Comparator.comparingInt(Dog::getDogWeight));  // Sort by weight  

, Comparator<Dog>, , , .

Comparator Dog, , Dog.

+1
source

All Articles