Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++
sort
stl

What is the std::sort() function in C++?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

std::sort() is a built-in function in C++'s Standard Template Library. The function takes in a beginning iterator, an ending iterator, and (by default) sorts the iterable in ascending order. The function can also​ be used for custom sorting by passing in a comparator function that returns a boolean.

Take a look at the function signature of std::sort() below:

svg viewer
Function signature of the std::sort()

Code

In the following examples, an array of strings will be sorted:

Using std::sort()

#include <iostream>
// The following library needs to be included to use std::sort()
#include <algorithm>
using namespace std;
int main() {
string coffee[] = {"latte", "cappuccino", "americano", "espresso"};
int size = 4;
std::sort(coffee, coffee + size);
for (int i = 0; i < 4; i++){
cout<<coffee[i]<<endl;
}
return 0;
}
Sorting the array elements in alphabetical order.

Using std::sort() with a built-in comparator

#include <iostream>
// The following library needs to be included to use std::sort()
#include <algorithm>
// The following library needs to be included to use std::greater<datatype>()
#include <functional>
using namespace std;
int main() {
string coffee[] = {"latte", "cappuccino", "americano", "espresso"};
int size = 4;
std::sort(coffee, coffee + size, std::greater<string>());
for (int i = 0; i < 4; i++){
cout<<coffee[i]<<endl;
}
return 0;
}
Sorting the array elements in reverse alphabetical order.

Using std::sort() with a custom comparator

#include <iostream>
// The following library needs to be included to use std::sort()
#include <algorithm>
using namespace std;
// Custom comparator to sort in descending order
bool comparator(string &a, string &b){
return a > b;
}
int main() {
string coffee[] = {"latte", "cappuccino", "americano", "espresso"};
int size = 4;
std::sort(coffee, coffee + size, comparator);
for (int i = 0; i < 4; i++){
cout<<coffee[i]<<endl;
}
return 0;
}
Sorting the array elements in reverse alphabetical order.

RELATED TAGS

c++
sort
stl
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring