0

If I have a struct and PQ is there a way for me to sort the heap by more than one variable. So first age, then if those are equal it does height.

Struct Person{
     int age;
     int height;
     int weight;
};

priority_queue<Person, vector<Person>, age_functor>

Struct age_functor{
     bool operator() (Person const& one, Person const& two){
          return one.age < two.age;
     }
}

The code will sort the queue by age but I'm wondering if I can somehow add in a secondary functor.

1 Answers1

2

Just compare age first, then compare height if ages are equal.

Struct age_height_functor{
     bool operator() (Person const& one, Person const& two){
          return
              one.age < two.age || // compare age
                  (one.age == two.age && // if ages are equal
                      one.height < two.height); // compare height
     }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70