Alternative way to sort a vector

This commit is contained in:
Koustav Chowdhury
2018-10-20 08:50:05 +05:30
committed by Kristofer Koishigawa
parent 2efc5a8e7b
commit d4848df011

View File

@ -214,6 +214,7 @@ int main()
``` ```
### Sorting Vector In Descending Order ### Sorting Vector In Descending Order
Sorting Vector in descending order can be done with the help of third argument namely greater<int>() in Sort() in C++. Sorting Vector in descending order can be done with the help of third argument namely greater<int>() in Sort() in C++.
``` cpp ``` cpp
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@ -233,5 +234,26 @@ int main(){
return 0; return 0;
} }
``` ```
An alternative way to do this.
``` cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> v{ 10, 5, 82, 69, 64, 70, 3, 42, 28, 0 };
sort(v.rbegin(), v.rend());
cout << "Vector Contents Sorted In Ascending Order:\n";
for(int e : v){
cout << e << " ";
}
return 0;
}
```
You can also sort in descending using lambda like the one above. You can also sort in descending using lambda like the one above.