0

Suppose we are living in a class named Person with instance variables name and age.

I would like to know whether it is possible to write a method that

  • sorts a list ascendingly with respect to name (lexicographical order) and age,
  • does not use Collections,
  • does use Comparator.

If I were to use Collections, my code would be as follows:

 public static void sortPeople(List<Person> people){
    Collections.sort(people,new Comparator<Person>(){
    @Override
    public int compare(Person p1, Person p2){

         if (p1.name.compareTo(p2.name)<0){
             return -1;
        } else if (p1.name.compareTo(p2.name)>0){
            return 1; 
        } else {
          return p1.age-p2.age;
        }
        }}); 
 }

But as I said, I would like to know if it is possible to do it differently.

Thanks for your attention!

RamPrakash
  • 1,687
  • 3
  • 20
  • 25

0 Answers0