Sort a Vector of Objects by Property in C++

When working with C ++ Standard Template Library (STL), Vector Container is one of the most commonly used data structures. Often, developers need to sort a vector of objects based on a particular feature. C ++ provides a powerful and flexible way to achieve this using Sorting the Headquarters. In this blog post, we will study how to sort the object vector using various properties such as the name, the output year and the status of retirement.

Object Sorting a Vector in C++

To arrange a vector of objects in C++, it is necessary to specify a custom sorting criterion. The sort function utilizes a comparison function that determines the arrangement order of the elements. This comparison function should return true if the first element is to be positioned before the second element.

Example: Sorting a Vector of Objects

Consider the class name Person with three properties

  1. name – this is data type of string and store the name of person.
  2. passoutYear for the storing the passout year of person and data type of integer.
  3. isRetiredĀ  ( bool ) – Wether the person is retired or not.

In this example we will create the vector of ‘Person’ objects and sort them based on different criteria.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Person {
public:
    string name;
    int passoutYear;
    bool isRetired;

    Person(string n, int pout, bool r){
        name = n;
        passoutYear = pout;
        isRetired = r;
    }

    void display() const {
        cout <<"Name : "<< name << " -|- Passout Year : " << passoutYear << " -|- Status : " 
             << (isRetired ? "Retired" : "Not Retired") << endl;
    }
};

bool sortByPassoutYear(const Person &a, const Person &b){
    return a.passoutYear < b.passoutYear;
}

bool sortByName(const Person &a, const Person &b){
    return a.name < b.name;
}

bool sortByRetirementStatus(const Person &a, const Person &b){
    return a.isRetired > b.isRetired;
}

int main(){
    vector<Person>people={
        Person("Asmita Khade", 2005, true),
        Person("Lucky Bisht", 2000, true),
        Person("Kushal Kashyap", 2008, false),
        Person("Rajeev Bharwan", 2012, false),
        Person("Vivek Jacob", 2017, false),
        Person("Kanwar", 1998, true),
        Person("Gaurav Arya", 2014, false)
    };

    cout<<"Sorting by Passingout Year:"<<endl;
    sort(people.begin(), people.end(), sortByPassoutYear);
    for (const auto &p : people){ 
      p.display();
    }
    
    cout << "\nSorting by Name:"<<endl;
    sort(people.begin(), people.end(), sortByName);
    for(size_t i = 0; i < people.size(); ++i) {
      const Person &p = people[i];
      p.display();
    }

    cout<<"\nSorting by Retirement Status:"<<endl;
    sort(people.begin(), people.end(), sortByRetirementStatus);
    for (const auto &p : people){
      p.display();
    }
    
    cout<<"\nSorting by Staus and Passout Year :"<<endl;
    sort(people.begin(), people.end(), [](const Person &a, const Person &b){
      if (a.isRetired != b.isRetired) 
            return a.isRetired;
        return a.passoutYear < b.passoutYear;   
    });
    for(size_t i = 0; i < people.size(); ++i){
      const Person &p = people[i];
      p.display();
    }
    return 0;
}

Used Sorting Methods

Sorting by pssoutYear –

sortByPassoutYear function is defined that returns true if first person’s pass-out year is less than the second person’s, this way sorting of the objects in ascending order will be done for passoutYear.

Sorting by Name

sortByName function is defined that returns true if first person’s name come before the second person’s name alphabetically, this way sorting of the objects in alphabetical order will be done for name.

Sorting by Retirement Status

If the first person is retired while the second is not then the true is return by the function sortByRetirementStatus. This will Lead to retrieve all the people first who are retired.

Sorting by Both Retirement Status and Passout Year

lambda function is used to first check if retirement status is different to prioritized retired people first. In the case of same status of retirement it will do sorting on the passoutYear.

Output –
Sorting by Passingout Year:
Name : Kanwar -|- Passout Year : 1998 -|- Status : Retired
Name : Lucky Bisht -|- Passout Year : 2000 -|- Status : Retired
Name : Asmita Khade -|- Passout Year : 2005 -|- Status : Retired
Name : Kushal Kashyap -|- Passout Year : 2008 -|- Status : Not Retired
Name : Rajeev Bharwan -|- Passout Year : 2012 -|- Status : Not Retired
Name : Gaurav Arya -|- Passout Year : 2014 -|- Status : Not Retired
Name : Vivek Jacob -|- Passout Year : 2017 -|- Status : Not Retired

Sorting by Name:
Name : Asmita Khade -|- Passout Year : 2005 -|- Status : Retired
Name : Gaurav Arya -|- Passout Year : 2014 -|- Status : Not Retired
Name : Kanwar -|- Passout Year : 1998 -|- Status : Retired
Name : Kushal Kashyap -|- Passout Year : 2008 -|- Status : Not Retired
Name : Lucky Bisht -|- Passout Year : 2000 -|- Status : Retired
Name : Rajeev Bharwan -|- Passout Year : 2012 -|- Status : Not Retired
Name : Vivek Jacob -|- Passout Year : 2017 -|- Status : Not Retired

Sorting by Retirement Status:
Name : Asmita Khade -|- Passout Year : 2005 -|- Status : Retired
Name : Kanwar -|- Passout Year : 1998 -|- Status : Retired
Name : Lucky Bisht -|- Passout Year : 2000 -|- Status : Retired
Name : Gaurav Arya -|- Passout Year : 2014 -|- Status : Not Retired
Name : Kushal Kashyap -|- Passout Year : 2008 -|- Status : Not Retired
Name : Rajeev Bharwan -|- Passout Year : 2012 -|- Status : Not Retired
Name : Vivek Jacob -|- Passout Year : 2017 -|- Status : Not Retired

Sorting by Staus and Passout Year :
Name : Kanwar -|- Passout Year : 1998 -|- Status : Retired
Name : Lucky Bisht -|- Passout Year : 2000 -|- Status : Retired
Name : Asmita Khade -|- Passout Year : 2005 -|- Status : Retired
Name : Kushal Kashyap -|- Passout Year : 2008 -|- Status : Not Retired
Name : Rajeev Bharwan -|- Passout Year : 2012 -|- Status : Not Retired
Name : Gaurav Arya -|- Passout Year : 2014 -|- Status : Not Retired
Name : Vivek Jacob -|- Passout Year : 2017 -|- Status : Not Retired

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top