In this section we will see how we can sort some array or linked list using standard library of C++. In C++ there are multiple different libraries that can be used for different purposes. The sorting is one of them.
The C++ function std::list::sort() sorts the elements of the list in ascending order. The order of equal elements is preserved. It uses operator< for comparison.
Example
#include <iostream>
#include <list>
using namespace std;
int main(void) {
list<int> l = {1, 4, 2, 5, 3};
cout << "Contents of list before sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
l.sort();
cout << "Contents of list after sort operation" << endl;
for (auto it = l.begin(); it != l.end(); ++it)
cout << *it << endl;
return 0;
}Output
Contents of list before sort operation 1 4 2 5 3 Contents of list after sort operation 1 2 3 4 5