What does it mean by saying "Comparable affects the original class but Comparator doesnt"

Viewed 1643
// Original class Dog
  class Dog{
   String name;
   int age;

}

//Case 1
    class Dog implements Comparable<Dog>{
             //compareTo() implementation
        }


//Case2
      class Dog implements Comparator<Dog>{
       // compare() implementation
    }

//Case 3

    class DogNameComparator implements Comparator<Dog>{
    // compare() implementation
}



 Collection.sort(dogList);
    Collectios.sort(dogList,new DogNameComparator());
    Collection.sort(dogList,new Dog());

Isnt it true that in case 2 the original class is actually modified even though they say Comparator doesnt modify the original class ?
May be if i have not understood the concept correctly, do enlighten me on it.

1 Answers
Related