Does the standard guarantee that order of equal elements will not change (eh, forgot the term for that) by using std::sort or do I need to consider an alternative solution to achieve this goal?
std::sort
is not guaranteed to be stable (the term you were trying to think of). As you'd guess, std::stable_sort
is guaranteed to be stable. std::stable_sort
also provides a guarantee on worst-case complexity, which std::sort
does not. std::sort
is typically faster on average though.
From C++ reference: here
Elements that would compare equal to each other are not guaranteed to keep their original relative order.
You might want stable_sort, but note that it's not as fast (in average)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With