language: C++ 4.7.2 (gcc-4.7.2)
date: 455 days 9 hours ago
link:
visibility: public
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
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <cstring>
using namespace std;
 
string remove_dot(const char *p)
{
    const char *dot = strchr(p, '.');
    return dot ? string(p, dot - p) : string(p);
}
 
bool compare_filenames(string a, string b)
{
    char *pA, *pB;
    long A = strtol(a.c_str(), &pA, 10),
         B = strtol(b.c_str(), &pB, 10);
    if (A < B)
        return true;
    if (A == B)
        return remove_dot(pA) < remove_dot(pB);
    return false;
}
 
int main(int, char **)
{
    const char *v[] ={
        "a10",
        "10a",
        "1ab",
        "2a",
        "1aa",
        "a2",
        "1bb",
        "1a",
        "a1",
        "1b",
        "10b",
        "2b",
        "b1",
        "b10"
    };
    vector<string> t(v, v+(sizeof(v)/sizeof(char*)));
    sort(t.begin(), t.end(), compare_filenames);
    copy(t.begin(), t.end(), ostream_iterator<string>(cout, "\n"));
    return 0;
}