-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringsort.cpp
48 lines (44 loc) · 984 Bytes
/
stringsort.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
#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>
using std::string;
using std::vector;
int nextnondigit (string a) {
for (unsigned int i=0;i<a.length();++i) {
if (!isdigit(a[i]))
return i;
}
return a.length();
}
class LexibackwardComparator {
public:
bool operator () (string a, string b) {
if (a.empty()||b.empty())
return false;
if (isdigit (a[0]) && isdigit(b[0])) {
int aint=0;
int bint=0;
sscanf (a.c_str(),"%d",&aint);
sscanf (b.c_str(),"%d",&bint);
if (aint==bint) {
return (*this)(a.substr(nextnondigit(a)),b.substr(nextnondigit(b)));
}
return aint<bint;
}
if (a[0]==b[0]) {
return (*this)(a.substr(1),b.substr(1));
}
return a[0]<b[0];
}
};
int main (int argc, char ** argv) {
vector <string> l;
for (int i=1;i<argc;++i) {
l.push_back(argv[i]);
}
std::sort (l.begin(),l.end(),LexibackwardComparator());
{for (unsigned int i=0;i<l.size();++i) {
printf ("%s\n",l[i].c_str());
}}
}