-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy path937-reorder-data-in-log-files.cpp
More file actions
36 lines (32 loc) · 1.05 KB
/
937-reorder-data-in-log-files.cpp
File metadata and controls
36 lines (32 loc) · 1.05 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
#include <algorithm>
#include <sstream>
#include <cctype>
class Solution {
public:
vector<string> reorderLogFiles(vector<string>& logs) {
vector<string> let_logs;
vector<string> dig_logs;
string id, first_word;
for (string log : logs) {
stringstream ss(log);
ss >> id;
ss >> first_word;
if (isalpha(first_word[0])) {
let_logs.push_back(log);
} else {
dig_logs.push_back(log);
}
}
sort(let_logs.begin(), let_logs.end(), [](const string& a, const string& b) {
string str_a = a.substr(a.find_first_of(" ") + 1);
string str_b = b.substr(b.find_first_of(" ") + 1);
if (str_a != str_b) {
return str_a < str_b;
} else {
return a < b;
}
});
let_logs.insert(let_logs.end(), dig_logs.begin(), dig_logs.end());
return let_logs;
}
};