C++ std::stable_sort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<algorithm> · C++98

Definition

In C++, std::stable_sort() sorts a range like std::sort(), but elements that compare equal stay in the same order as before.

This matters when you sort records by one field and want earlier sorting to stay visible, for example sorting tasks by priority but keeping the original order within each priority.

Syntax

Add #include <algorithm>, then call std::stable_sort(). Start with these two lines.

C++
std::stable_sort(v.begin(), v.end());
std::stable_sort(v.begin(), v.end(), comp);
CallWhat it does
std::stable_sort(v.begin(), v.end());Sort ascending and keep ties in order.
std::stable_sort(v.begin(), v.end(), comp);Sort with your own rule.
Library declarations
C++
template<class RandomIt>
void stable_sort(RandomIt first, RandomIt last);

template<class RandomIt, class Compare>
void stable_sort(RandomIt first, RandomIt last, Compare comp);

Parameters

ParameterDescription
first, lastRequired. The range to sort.
compOptional. A function that returns true when its first argument should come first.

Return Value

Return ValueDescription
voidNothing is returned. The range is sorted in place.

C++ Examples

Example 1 — Keep the order of ties

Sort tasks by priority without shuffling tasks of equal priority.

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

struct Task {
    std::string name;
    int priority;
};

int main() {
    std::vector<Task> tasks = {{"write", 2}, {"test", 1}, {"deploy", 2}, {"plan", 1}};

    std::stable_sort(tasks.begin(), tasks.end(),
                     [](const Task& a, const Task& b) { return a.priority < b.priority; });

    for (const Task& t : tasks) {
        std::cout << t.name << ' ' << t.priority << '\n';
    }
    return 0;
}

How It Works

  1. The lambda compares only the priority number.
  2. test came before plan in the input, and both have priority 1, so they stay in that order.
  3. The same is true for write and deploy with priority 2.

Example 2 — Sort words by length

Words of the same length keep their original order.

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"pear", "fig", "apple", "kiwi", "plum"};

    std::stable_sort(words.begin(), words.end(),
                     [](const std::string& a, const std::string& b) { return a.size() < b.size(); });

    for (const std::string& x : words) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. fig is the shortest word, so it goes first.
  2. pear, kiwi and plum all have four letters and keep the input order.
  3. apple is the longest, so it goes last.

Example 3 — Sort by absolute value

Ties between numbers like 1 and -1 keep their input order.

C++
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {-3, 1, 3, -1, 2};

    std::stable_sort(v.begin(), v.end(),
                     [](int a, int b) { return std::abs(a) < std::abs(b); });

    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

How It Works

  1. The comparison looks at std::abs(a), so signs are ignored.
  2. 1 and -1 tie, and 1 appeared first, so it stays first. Same for -3 and 3.
  3. A plain sort() could print -1 1 or 1 -1. stable_sort() always gives 1 -1.

Frequently Asked Questions

In C++, std::stable_sort() sorts a range and keeps the order of equal elements. Include <algorithm> to use it.
Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
It returns nothing (void). The range is sorted in place, and equal elements keep their original order.
Use it when equal elements must keep their original relative order, for example when you sort records by several fields in turn.
It can be a little slower or use extra memory. For most small programs the difference does not matter.

Next: swap()

Swaps the values of two objects.

swap() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful