01The comparator is the order
std::sort(v.begin(), v.end(), comp) runs an O(n log n) algorithm (introsort), and the only thing it knows about your data is your function comp(a, b), which answers: should a come before b?
That means you can sort by any rule:
- descending:
return a > b; - by a field:
return a.score > b.score; - by several keys: compare the first key; only if equal, compare the next
In other languages it looks different but works the same: Python sorted(v, key=lambda p: (-p.score, p.name)), Java Comparator.comparing(...). A key function returning a tuple is often the easiest way to get orders on several keys right.