-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path03_sorting.cpp
59 lines (48 loc) · 1.05 KB
/
03_sorting.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Sorting
*/
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool compare(string s1, string s2){
if(s1.length() == s2.length()){ // if length is same, then compare lexicographic
return s1<s2;
}
return s1.length()<s2.length(); // comparing string lengths
}
int main(){
int totalStr;
cout << "Enter total number of strings: ";
cin >> totalStr;
cin.get();
string s[100];
cout << "Enter Strings: \n";
for(int seq=0; seq <= totalStr-1; seq++){
getline(cin,s[seq]);
}
cout << "\nAfter Sorting: " << endl;
sort(s, s+totalStr, compare); // sorting
for(int seq=0; seq <= totalStr-1; seq++){
cout << s[seq] << endl;
}
return 0;
}
/*
OUTPUT:
Enter total number of strings: 6
Enter Strings:
Amandeep Verma
deep
aman
Govind
Arvind
Bhim Gupta
After Sorting:
aman
deep
Arvind
Govind
Bhim Gupta
Amandeep Verma
*/