mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-08 04:15:23 +03:00
sort: Add an option to sort in reverse order
Lines can now be sorted in reverse order by specifying the `-r` option.
This commit is contained in:
parent
f5f1a5228e
commit
0454d655bb
Notes:
sideshowbarker
2024-07-17 04:34:25 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/0454d655bb Pull-request: https://github.com/SerenityOS/serenity/pull/17813 Reviewed-by: https://github.com/AtkinsSJ ✅
@ -55,6 +55,7 @@ struct Options {
|
||||
size_t key_field { 0 };
|
||||
bool unique { false };
|
||||
bool numeric { false };
|
||||
bool reverse { false };
|
||||
StringView separator { "\0", 1 };
|
||||
Vector<DeprecatedString> files;
|
||||
};
|
||||
@ -104,6 +105,7 @@ ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments)
|
||||
args_parser.add_option(options.unique, "Don't emit duplicate lines", "unique", 'u');
|
||||
args_parser.add_option(options.numeric, "treat the key field as a number", "numeric", 'n');
|
||||
args_parser.add_option(options.separator, "The separator to split fields by", "sep", 't', "char");
|
||||
args_parser.add_option(options.reverse, "Sort in reverse order", "reverse", 'r');
|
||||
args_parser.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
@ -120,9 +122,15 @@ ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments)
|
||||
|
||||
quick_sort(lines);
|
||||
|
||||
for (auto& line : lines) {
|
||||
outln("{}", line.line);
|
||||
}
|
||||
auto print_lines = [](auto const& lines) {
|
||||
for (auto& line : lines)
|
||||
outln("{}", line.line);
|
||||
};
|
||||
|
||||
if (options.reverse)
|
||||
print_lines(lines.in_reverse());
|
||||
else
|
||||
print_lines(lines);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user