We have discussed qsort() in C. C++ STL provides a similar function sort that sorts a vector or array (items with random access). Below is a simple program to show working of sort().
Array after sorting using default sort is : 0 1 2 3 4 5 6 7 8 9
So by default, sort() sorts an array in ascending order.
How to sort in descending order?
sort() takes a third parameter that is used to specify the order in which elements are to be sorted. We can pass “greater()” function to sort in descending order. This function does comparison in a way that puts greater element before.
Array after sorting : 9 8 7 6 5 4 3 2 1 0
How to sort in particular order?
We can also write our own comparator function and pass it as a third parameter.
Intervals sorted by start time : [1,9] [2,4] [4,7] [6,8]
This article is contributed by Shubham Agrawal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Count the triplets such that A[i] < B[j] < C[k]
- Check whether an array can be made strictly increasing by modifying atmost one element
- Check if the given matrix is increasing row and column wise
- Check if the given array contains all the divisors of some integer
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Find number from its divisors
- Find the node whose sum with X has minimum set bits
- Salt and Pepper Noise removal using C++
- Find the largest interval that contains exactly one of the given N integers.
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Dynamic Constructor in C++ with Examples
- Sort an array according to count of set bits | Set 2
- AKTU (UPTU) Previous Year Solved Papers | C Programming
- Count of integers of the form (2^x * 3^y) in the range [L, R]
- C++ Stream Classes Structure
Improved By : rahuku