Here’s another one for my lab group at Abertay University. In these series of articles, I proceed to answer some interesting questions that some of the students have asked. This time, the question is:
How do I sort an std::list of objects?
When using the STL list, providing that the items in your list can be compared (i.e. they override the comparison operators ‘<’, ‘>’, etc), then you can sort the elements of your list in ascending order using the list’s sort method:
Lets now imagine we have a collection of ‘Employee’ objects, and we would like to sort by ‘age’:
The STL doesn’t know how to sort these objects by default, and we must therefore tell it how to do so.
The STL provides an overload (read alternative version) of the list’s ‘sort’ method that allows you to specify a function for it to use to compare the items in your list. The function that ‘sort’ expects must conform to the following rules:
- It must have two arguments and these arguments must be of the same type as the items in your list
- It must return a boolean to indicate which of the two arguments should be placed first in the sorted list:
- It should return true if the object passed in as the first argument should appear before the object passed in as the second argument
- Or false otherwise
With this in mind, we can create a compatible function to sort our ‘Employee’ objects:
Now that we have a function that is compatible with the STL list’s ‘sort’ method, we can pass the name of this function in as an argument to the ‘sort’ method as shown below: