how to sort array in descending order in c++ code example
Example 1: java treemap sort by value in reverse
Map<String, Integer> unSortedMap = getUnSortedMap();
System.out.println("Unsorted Map : " + unSortedMap);
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unSortedMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
System.out.println("Sorted Map : " + sortedMap);
Output:
Unsorted Map : {alex=1, charles=4, david=2, brian=5, elle=3}
Sorted Map : {alex=1, david=2, elle=3, charles=4, brian=5}
Example 2: how to sort in descending order c++
int arr[10];
int length = sizeof(arr)/sizeof(arr[0]);
sort(arr, arr+length, greater<int>());
Example 3: sort in descending order c++ stl
sort(arr, arr + n, greater<int>())
Example 4: how to sort an array in c++
#include <bits/stdc++.h>
using namespace std;
#define size(arr) sizeof(arr)/sizeof(arr[0]);
int main(){
int a[5] = {5, 2, 6,3 ,5};
int n = size(a);
sort((a), a + n);
for(int i = 0; i < n; i++){
cout << a[i];
}
return 0;
}
Example 5: how to sort in descending order in c++
sort(str.begin(), str.end(), greater<int>());
cout<<str;
Example 6: stl sort in c++
sort(arr, arr+n);