Answers for "how to sort array in descending order in c++"

12

how to sort in descending order c++

int arr[10];
int length = sizeof(arr)/sizeof(arr[0]); 
sort(arr, arr+length, greater<int>());
Posted by: Guest on May-16-2020
2

java treemap sort by value in reverse

Map<String, Integer> unSortedMap = getUnSortedMap();
         
System.out.println("Unsorted Map : " + unSortedMap);
 
//LinkedHashMap preserve the ordering of elements in which they are inserted
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}
Posted by: Guest on March-01-2020
6

sort in descending order c++ stl

sort(arr, arr + n, greater<int>())
Posted by: Guest on July-19-2020
5

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;

}
Posted by: Guest on March-18-2020
5

stl sort in c++

sort(arr, arr+n); // sorts in ascending order
Posted by: Guest on June-01-2020
2

how to sort in descending order in c++

sort(str.begin(), str.end(), greater<int>());
cout<<str;
Posted by: Guest on December-11-2020

Code answers related to "how to sort array in descending order in c++"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language