Support Online
Skip to main content

C++ std::sort() Usage

What Will You Learn in This Guide?

In this guide, you'll learn how the std::sort() function in C++ works, what algorithm it uses, and how you can sort arrays ascending, descending, or by a special rule.
You'll test code with examples and explore advanced techniques like std::greater, lambda expressions, and custom functions.

What is std::sort() and How Does It Work?

std::sort() is a built-in function in the C++ Standard Library (<algorithm>) that is used to sort data structures (e.g. arrays or vectors) in a specific order.

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

first → Beginning of the range to be sorted

last → Address showing the next element after the last element

comp → (optional) Comparison function. If not entered, std::less<T>() is used by default (ascending order).

  • Algorithm used: std::sort() uses Introsort, a hybrid technique (mixture of Quick Sort + Heap Sort + Insertion Sort). Its complexity is average and in the worst case O(N log N).

1. Sort Ascending (Default)

By default, std::sort() sorts arrays in ascending order.


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int sayilar[] = {50, 40, 30, 20, 10};
int uzunluk = sizeof(sayilar)/sizeof(sayilar[0]);

std::sort(sayilar, sayilar + uzunluk); // Artan sıralama

for (int i = 0; i < uzunluk; i++)
cout << sayilar[i] << " ";
}
  • Description:

If the third parameter is not specified, std::less<int>() is used and sorts the array from smallest to largest.


2. Sort in Descending Order

For descending sorting, the third parameter is given the function std::greater<int>() or lambda.

Method 1 – std::greater<int>()


int sayilar[] = {44, 22, 55, 33, 66};
std::sort(sayilar, sayilar + 5, greater<int>()); // Büyükten küçüğe

Method 2 – Lambda Function


std::sort(sayilar, sayilar + 5, [](int a, int b){ return a > b; });
Note:

Both methods give the same result — they sort the array in descending order.


3. Custom Sort (User Defined)

You can also define your own comparison rule. In the example below, we sort the array by remainder after division by 10:


#include <iostream>
#include <algorithm>
using namespace std;

bool kalanGoreSırala(int a, int b) {
return (a % 10) < (b % 10);
}

int main() {
int sayilar[] = {18, 45, 34, 92, 21};
std::sort(sayilar, sayilar + 5, kalanGoreSırala);
for (int s : sayilar) cout << s << " ";
}
  • Description:

The element with the smallest value (a 10%) is brought forward. Result: 21, 92, 34, 45, 18.


Performance and Complexity

FeatureDescription
AlgorithmIntrosort (combination of Quick Sort + Heap Sort + Insertion Sort)
Average ComplexityO(N log N)
Worst CaseO(N log N)
Stability (stable)No — items with the same values ​​can be swapped after sorting

If stable sorting is required:

You can use std::stable_sort() function.


Frequently Asked Questions (FAQ)

  1. Does std::sort() only work on arrays?

No, it also works with random access structures like std::vector and std::deque.

  1. What is the difference with qsort()?

std::sort() is a type-safe, modern version of C++. qsort() belongs to the C language and works with void* pointers.

  1. Can I also sort strings?

Yes, you can sort std::string or char arrays as well.

  1. Why should I return bool?

The comparator function specifies true/false which of the two elements should come first.

  1. Why is std::sort() not stable?

The order of elements with the same value is not preserved. So std::stable_sort() is recommended if stability is required.


Result

In this guide, we examined the C++ std::sort() function from basic to advanced level. You can sort by ascending, descending or special rules; You can customize it with lambda expressions. Thanks to the Introsort algorithm, your rankings will be fast, reliable and flexible.

Try your C++ projects on the GenixNode platform now and observe the performance difference.