Skip to content

Sorting

Read · 1 of 4

One line to sort

sort(a.begin(), a.end()); puts a vector in increasing order (add #include <algorithm>). Add greater<int>() as a third argument for decreasing order.

To sort by your own rule, pass a function that says whether x should come before y: sort(v.begin(), v.end(), [](const Student& x, const Student& y) { return x.marks > y.marks; });