mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 22:02:44 +00: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
					
				
					 1 changed files with 11 additions and 3 deletions
				
			
		|  | @ -55,6 +55,7 @@ struct Options { | ||||||
|     size_t key_field { 0 }; |     size_t key_field { 0 }; | ||||||
|     bool unique { false }; |     bool unique { false }; | ||||||
|     bool numeric { false }; |     bool numeric { false }; | ||||||
|  |     bool reverse { false }; | ||||||
|     StringView separator { "\0", 1 }; |     StringView separator { "\0", 1 }; | ||||||
|     Vector<DeprecatedString> files; |     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.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.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.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.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No); | ||||||
|     args_parser.parse(arguments); |     args_parser.parse(arguments); | ||||||
| 
 | 
 | ||||||
|  | @ -120,9 +122,15 @@ ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     quick_sort(lines); |     quick_sort(lines); | ||||||
| 
 | 
 | ||||||
|     for (auto& line : lines) { |     auto print_lines = [](auto const& lines) { | ||||||
|         outln("{}", line.line); |         for (auto& line : lines) | ||||||
|     } |             outln("{}", line.line); | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     if (options.reverse) | ||||||
|  |         print_lines(lines.in_reverse()); | ||||||
|  |     else | ||||||
|  |         print_lines(lines); | ||||||
| 
 | 
 | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tim Ledbetter
						Tim Ledbetter