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
- Full screen OpenCV / GtK application in C++ running on Raspberry PI
- C++, OpenCV and Gtk3 Windows dev environment
- Check if words are sorted according to new order of alphabets
- How to create an unordered_map of user defined class in C++?
- Ceiling of every element in same array
- Floor of every element in same array
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- How to delete last element from a set in C++
- attributes in C++
- Print numbers in descending order along with their frequencies
- Find sum of a number and its maximum prime factor
- How to traverse a C++ set in reverse direction
- How to Read and Print an Integer value in C++
- How to Read and Print an Integer value in C
- Difference between Definition and Declaration
Improved By : rahuku