STL Algorithm – std::sort Tutorial and Example

STL Algortihm ( std :: sort)

std sort: This article is all about std::sort algorithm. This std::sort is a  algorithm which provides the functionality of sorting a given range of elements. To use this, we need to pass start and end of range in it as an argument .

std::sort (<START OF RANGE> , <END OF RANGE>);

Let’s see some example to understand this more clearly.

Example 1 : Sorting an integer array vector :

Let’s take a example, we have an array of integers and we want to sort them using std::sort we can do this by following method.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
int main() {
    // initializing the vector array 
int arr1[] = { 10, 30, 20, 80, 60, 40, 70, 50 };
int len = sizeof(arr1) / sizeof(int);
std::sort(arr1, arr1 + len);
std :: cout << " Sorted Vectors : " ;
for (int x = 0; x< len; x++)
std::cout << arr1[x] << " , ";
std::cout << std::endl;
return 0;
}
Output :

Sorted Vectors : 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80

Example 2 : Sorting the vector of string :

When we want to sort vector of string ‘’ std::sort ‘’ will use the < operator for sorting provided range of strings, so given string elements will be sorted in lexicographical order.

#include <string>
int main() {
    // initializing the vector array 
std::vector<std::string> vec;
vec.push_back("bcd");
vec.push_back("egf");
vec.push_back("aaa");
vec.push_back("caa");
vec.push_back("abc");
// sorting the array vector 
std::sort(vec.begin(), vec.end());
// printing sorted array 
std :: cout << "Sorted vectors : " ;
std::for_each(vec.begin(), vec.end(),
[](std::string s ) {
std::cout<<s << "   ";
});
std::cout << std::endl;
}
Output :

Sorted vectors : aaa    abc   bcd   caa    egf

Example 3 : Sorting of vector  based on ID :

Let we have a class with name and id as member variables. Now we want to sort a vector of class Person objects on the basis of id. We can do this by following method .

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
class People {
public:
std::string MN;
int ID;
People(std::string name, int id) :
MN(name), ID(id) {}
bool operator <(const People & obj)
{
 	if (ID < obj.ID)
 		return true;
 	else
 	 	return false;
}  };
int main() {
// initializing the objects of vector 
std::vector<People> vecpos = { People("aaa", 1), People("ccc", 3),
People("bbb", 2), People("ddd", 4 ) };
std::sort(vecpos.begin(), vecpos.end());
// printing the sorted vector 
std::cout << "Sorting of vector  based on ID ...\n";
std::for_each(vecpos.begin(), vecpos.end(), [](People & obj) {
std::cout<<obj.ID<< " ---> "<<obj.MN<<std::endl;
}); }
Output :

Sorting of vector  based on ID ...
1 ---> aaa
2 ---> bbb
3 ---> ccc
4 ---> ddd

Example 4 : Sorting vector List based on Name using Function Object :

When  we want to sort  a vector of class objects based on its name instead of id &
Also we  cannot change the current implementation of < operator in class. Then in such scenario we will use overloaded version of std::sort that accepts a Function Pointer as an argument and uses that for comparisons instead of < operator.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
class People {
public:
std::string MN;
int ID;
People(std::string name, int id) :
MN(name), ID(id) {
}
bool operator <(const People & obj) {
if (ID < obj.ID)
return true;
else
return false;
}
};
struct poscomp {
bool operator()(const People & first, const People & sec) {
if (first.MN < sec.MN)
return true;
else
return false;
} };
int main()
{   
    // initilazation of objects
    std::vector<People> vecpos = { People("aaa", 1), People("ccc", 3),People("bbb", 2), People("ddd", 4 ) };
    //sorting 
    std::sort(vecpos.begin(), vecpos.end(), poscomp());
    //printing of sorted objects 
std::cout << "Sorting vector List based on Name using Function Object ....\n";
std::for_each(vecpos.begin(), vecpos.end(), [](People & obj) {
std::cout<<obj.ID<< "--> "<<obj.MN<<std::endl;
});
}
 Output :
Sorting vector List based on Name using Function Object ....
1 --> aaa
2 --> bbb
3 --> ccc
4 --> ddd