sorting ascending and descending

how to sorting ascending and descending? i really not understand, this is my code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>

using namespace std;
int main()
{
string Huruf[] = {"A", "B", "C", "D", "E"};
int arr[10] = { 10, 25, 30, 40, 30, 25, 40, 10, 15, 15 };
int no[] = {1, 2, 3, 4, 5, 0};

std::vector<std::string> v;

//A-B = 10, A-C = 25, A-D = 30, A-E = 40, B-C = 30, B-D = 25, B-E = 40, C-D = 10, D-E = 15, C-E = 15.
cout<<"Sisi-sisinya = ";
for (int i=0; i<5; i++){
	for (int j=i+1; j<5; j++){
	 v.push_back(Huruf[i]+"-"+Huruf[j]);
	}
}
for (int ii=0; ii<10; ii++){
	cout<<v[ii]<<"="<<arr[ii]<<" ";
}

}


if your running code thats result :
" Sisi-sisinya = A-B=10 A-C=25 A-D=30 A-E=40 B-C=30 B-D=25 B-E=40 C-D=10 C-E=15 D-E=15 "
i want to sorting ascending and descending ..
Last edited on
http://www.cplusplus.com/reference/algorithm/sort/

the default comparison is to sort ascending (a,b,c) but if you write your own inverted one it will sort decending, simply return true if left > right instead of the default true of left < right. or as the example shows it just returns a comparison: return (left > right); is reverse.

Topic archived. No new replies allowed.