Home C++ Algorithms Functions stable_sort() C++ std::stable_sort() Function 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.
std::stable_sort(v.begin(), v.end());
std::stable_sort(v.begin(), v.end(), comp); Call What 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 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 Parameter Description first, last Required. The range to sort. comp Optional. A function that returns true when its first argument should come first.
Return Value Return Value Description voidNothing is returned. The range is sorted in place.
C++ Examples 1. Keep the order of ties 2. Sort words by length 3. Sort by absolute value Example 1 — Keep the order of ties Sort tasks by priority without shuffling tasks of equal priority.
#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;
} test 1
plan 1
write 2
deploy 2 How It Works The lambda compares only the priority number. test came before plan in the input, and both have priority 1, so they stay in that order.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.
#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 fig is the shortest word, so it goes first.pear, kiwi and plum all have four letters and keep the input order.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.
#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 The comparison looks at std::abs(a), so signs are ignored. 1 and -1 tie, and 1 appeared first, so it stays first. Same for -3 and 3. A plain sort() could print -1 1 or 1 -1. stable_sort() always gives 1 -1. Frequently Asked Questions What does std::stable_sort() do? In C++, std::stable_sort() sorts a range and keeps the order of equal elements. Include <algorithm> to use it.
Which header and C++ version do I need for std::stable_sort()? Include <algorithm>. It has been part of C++ since C++98, so every modern compiler supports it.
What does std::stable_sort() return? It returns nothing (void). The range is sorted in place, and equal elements keep their original order.
When should I use stable_sort() instead of sort()? Use it when equal elements must keep their original relative order, for example when you sort records by several fields in turn.
Is stable_sort() slower than sort()? It can be a little slower or use extra memory. For most small programs the difference does not matter.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion