Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08112b9

Browse filesBrowse files
committed
added sorting by file name
1 parent 41b63f1 commit 08112b9
Copy full SHA for 08112b9

File tree

3 files changed

+13
-3
lines changed
Filter options

3 files changed

+13
-3
lines changed

‎main.cpp

Copy file name to clipboardExpand all lines: main.cpp
+3-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ int main(int argc, char* argv[]) {
1111
// CLI flag vars
1212
std::string dir = ".";
1313
bool gdf = false;
14+
bool sort = false;
1415

1516
app.add_option("Directory", dir, "Input a file");
1617
app.add_flag("-g,--group-directories-first", gdf, "Do you want dirs first?");
18+
app.add_flag("-s,--sort", sort, "Sort by name");
1719
CLI11_PARSE(app, argc, argv);
1820

19-
(new Directory(dir))->show_ls(gdf);
21+
(new Directory(dir, sort))->show_ls(gdf);
2022

2123
return 0;
2224
}

‎utils/types.cpp

Copy file name to clipboardExpand all lines: utils/types.cpp
+9-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ File get_file_info(const fs::directory_entry& file) {
154154
);
155155
}
156156

157-
Directory :: Directory (const std::string& path) {
157+
bool sort_compare_file(File f1, File f2) {
158+
return f1.get_path().path().filename().string() < f2.get_path().path().filename().string();
159+
}
160+
161+
Directory :: Directory (const std::string& path, bool sort) {
158162
std::vector<File> paths_h = {};
159163

160164
if (fs::directory_entry(path).exists() && !fs::directory_entry(path).is_directory()) {
@@ -168,6 +172,9 @@ Directory :: Directory (const std::string& path) {
168172
}
169173
}
170174

175+
if (sort)
176+
std::sort(paths_h.begin(), paths_h.end(), sort_compare_file);
177+
171178
this->paths = paths_h;
172179
}
173180

@@ -231,6 +238,7 @@ void Directory :: show_ls(bool gdf) {
231238
std::vector<File> dirs;
232239
std::vector<File> others;
233240

241+
234242
if (gdf) {
235243
for (File file : paths_h) {
236244
if (file.get_path().is_directory()) {

‎utils/types.h

Copy file name to clipboardExpand all lines: utils/types.h
+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Directory {
7676
private:
7777
std::vector<File> paths;
7878
public:
79-
explicit Directory(const std::string&);
79+
explicit Directory(const std::string&, bool);
8080
~Directory() = default;
8181

8282
std::vector<File> get_paths();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.