Many times I have to use a custom compare function for sorting.
I can Implement it but Sometimes I make a mistake. I usually get confused if it returns true
then it will be swapped or not? Can someone explain what happens if it returns true
and what if it returns false
?

- 57,834
- 11
- 73
- 112
-
5Did you read the documentation of [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort)? "_comparison function object (i.e. an object that satisfies the requirements of Compare) which returns `true` if the first argument is less than (i.e. is ordered before) the second._" – Algirdas Preidžius Jun 12 '20 at 12:03
-
2a comparator the returns either `true` or `false` always cannot be used with `std::sort` (no strict weak ordering). I suppose you mean `a < b` usually implies that the comparator returns `true`, the opposite of that is `b <= a` which is also not valid as comparator. Please clarify your question – 463035818_is_not_an_ai Jun 12 '20 at 12:05
-
5Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – cigien Dec 30 '20 at 23:56
-
@largest_prime_is_463035818 the question is totally clear and simple, Algirdas nailed it. Do not complicate it, it brings confusion. – Tomas Dec 31 '20 at 00:02
2 Answers
I usually get confused if it returns
true
then it will be swapped or not?
That’s irrelevant, and you don’t (need to) know.
The only thing that’s relevant is that the comparator should return true
if and only if the first parameter is strictly less than the second parameter (i.e. it should come before, after sorting). In other words, it should return the same result as <
logically would.

- 530,221
- 131
- 937
- 1,214
-
but in a custom comparator, you are the one who decides which is less and which is more. say you have a function ```static bool comp(string a, string b){ if(isdigit(a[0]))return true; else return false; }```, does return true swap the strings or does return false swap the strings? – Deus Ex Jun 20 '20 at 20:18
-
@DeusEx Neither. This isn't a valid less-than relation. It violates the [strict weak ordering](https://en.m.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings) contract imposed by `std::sort` etc., and it *will break* those functions. – Konrad Rudolph Jun 20 '20 at 21:33
The custom compare function doesn't swap things, nor signal when a swap should take place. That's the algorithm's job, and you don't need to worry about that.
The custom compare function compares things. It's the way you tell the algorithm which elements are to be deemed "less than" other elements. That's all it needs you to tell it, in order for it to go off and do its job sorting.
Read documentation/your book for the functions you use. The requirements for std::sort
are freely available to study:
comp
: comparison function object (i.e. an object that satisfies the requirements ofCompare
) which returns true
if the first argument is less than (i.e. is ordered before) the second.

- 17,071
- 2
- 21
- 35
-
but in a custom comparator, you are the one who decides which is less and which is more. say you have a function ```static bool comp(string a, string b){ if(isdigit(a[0]))return true; else return false; }```, does return true swap the strings or does return false swap the strings? – Deus Ex Jun 20 '20 at 20:22
-
@DeusEx Neither. The comparator doesn't determine when a swap occurs. The algorithm does. It's not `shallISwapTheseThings`; it's `shallIDeemThisThingToBeLessThanThisOtherThing`. I suggest studying on how sorting algorithms work. – Asteroids With Wings Jun 20 '20 at 22:39
-
aren't we just arguing technicality? the sorting algorithm does the swap, but the comparator defines the < operator. So if you define (a – Deus Ex Jun 23 '20 at 06:34
-
@DeusEx In that very very simple example yes, but the two things are not directly connected, and in a real use case you'll see many many more occasions when `return true` might _or might not_ result in a swap depending on the state of the sorting algorithm. It's much more accurate (and much easier) to describe the comparator as doing exactly what it does (define a sort ordering) than confusing matters with anything else. – Asteroids With Wings Jun 23 '20 at 14:27
-
@DeusEx If I tell you "the house you're looking for is next to the field", sure sometimes that might mean "turn left", but not always, as it depends on your direction of travel, so instead we just say _where_ the house is and not how to get there. Your satnav (or, ideally, brain!) does the part that calculates directions. – Asteroids With Wings Jun 23 '20 at 14:28