-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorderDatainLogFiles.cpp
More file actions
45 lines (44 loc) · 1.21 KB
/
ReorderDatainLogFiles.cpp
File metadata and controls
45 lines (44 loc) · 1.21 KB
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
#include<iostream>
#include<vector>
#include<algorithm>
bool compare(const std::string &log1, const std::string &log2){
//determine if log1 and log2 is number or word
bool log1IsAlpha = isalpha(log1.back());
bool log2IsAlpha = isalpha(log2.back());
if(log1IsAlpha==log2IsAlpha){
if(!log1IsAlpha){
//both are number type
return false;
}
else{
//both are word type
int index1 = log1.find(' ') + 1;
int index2 = log2.find(' ') + 1;
std::string suffix1 = log1.substr(index1);
std::string suffix2 = log2.substr(index2);
if(suffix1!=suffix2){
return suffix1<suffix2;
}
else{
return log1<=log2;
}
}
}
//they are different
if(log1IsAlpha){
return true;
}
return false;
}
std::vector<std::string> reorderLogFiles(std::vector<std::string>& logs) {
std::stable_sort(logs.begin(),logs.end(),compare);
return logs;
}
int main(){
std::vector<std::string> vec = {"dig2 3 6","dig1 8 1 5 1"};
vec = reorderLogFiles(vec);
for(auto s: vec){
std::cout<<s<<std::endl;
}
return 0;
}