Introduction
Sorting is a common operation in programming, and C++ provides various ways to sort data. By default, C++’s sorting functions rely on the comparison operator (<
), which works well for most cases. However, there are situations where we may need to define custom sorting rules, particularly when working with user-defined data types or complex structures.
In this article, we will explore how to perform custom sorting using literals in C++. We will look at an example scenario where we have an array of objects and want to sort them based on a specific attribute or property.
Example Scenario
Let’s say we have a Person
class with attributes like name
, age
, and salary
. We have an array of Person
objects, and we want to sort them based on their age in ascending order.
class Person {
public:
std::string name;
int age;
double salary;
// Assume the constructor and other member functions are defined
};
std::vector<Person> people {
{"John", 30, 5000.0},
{"Alice", 25, 4000.0},
{"Bob", 35, 6000.0},
// ...more Person objects
};
Custom Sorting using Literals
To perform custom sorting based on the age
attribute, we can leverage C++’s sort algorithm and provide a custom comparison function using a literal.
bool sortByAge(const Person& p1, const Person& p2) {
return p1.age < p2.age;
}
std::sort(people.begin(), people.end(), sortByAge);
In the above code snippet, we define a function sortByAge
that takes two Person
objects as arguments and compares their ages using the less-than operator (<
). This function returns true
if the age of the first person is less than the age of the second person.
Then, we use std::sort
function from the algorithm library to sort the people
vector based on the custom comparison function sortByAge
. The std::sort
function rearranges the elements of the vector in ascending order according to the provided comparison function.
Conclusion
By using custom sorting with literals in C++, we can easily sort data based on specific attributes or properties of user-defined types. This allows us to have more control over the sorting process and tailor it to our specific requirements.
Understanding how to utilize custom sorting is valuable when working with complex data structures and enables us to efficiently organize and manipulate data in our programs.
References:
#cplusplus #sorting