Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Text.RegularExpressions;
- namespace Sort
- {
- public class Sort
- {
- public static bool CheckCommand(string command)
- {
- switch (command)
- {
- case "sort":
- return true;
- case "Sort":
- Console.WriteLine("I don't know this command, sorry to say that. Is there a chance you meant 'sort'?");
- return false;
- default:
- Console.WriteLine("I don't know this command, sorry to say that. And I have no idea what you meant...");
- return false;
- }
- }
- public static List<String> ReadFile(string name)
- {
- List<String> lines = new List<String>();
- try
- {
- foreach (string line in File.ReadLines(name))
- {
- lines.Add(line);
- }
- }
- catch (FileNotFoundException)
- {
- Console.WriteLine("I am so sorry to say, but I cannot find such file (" + name + "). I know this might be very difficult," +
- " but please check if you have entered the name correctly. I believe in you.");
- }
- return lines;
- }
- public static async Task Print(bool toFile, List<String> linesArray, string fileName)
- {
- if (toFile)
- {
- await File.WriteAllLinesAsync(fileName, linesArray);
- }
- else
- {
- foreach (var line in linesArray)
- {
- Console.WriteLine(line);
- }
- }
- }
- public static List<String> SortLines(List<String> lines, bool reverse, bool unique, bool numericSort)
- {
- if (numericSort)
- {
- List<String> nums = new List<string>();
- List<String> other = new List<string>();
- foreach (var line in lines)
- {
- if (new Regex("^[0-9]*$").IsMatch(line)) nums.Add(line);
- else other.Add(line);
- }
- nums.Sort();
- if (reverse) nums.Reverse();
- nums.AddRange(other);
- if (unique) nums = nums.Distinct().ToList();
- return nums;
- }
- lines.Sort();
- if (reverse) lines.Reverse();
- if (unique) lines = lines.Distinct().ToList();
- return lines;
- }
- public static void Main(string[] args)
- {
- var command = Console.ReadLine();
- String[] flags = command!.Split(" ");
- if(!CheckCommand(flags[0])) return;
- var fileName = flags[^1];
- var linesArray = ReadFile(fileName);
- bool ignoreCase = false,
- reverse = false,
- unique = false,
- numericSort = false,
- versionSort = false,
- fileOutput = false;
- string outputFileName = "output.txt";
- for (int i = 1; i < flags.Length - 1; i++)
- {
- var flag = flags[i];
- if (flag == "-f" || flag == "--ignore-case") ignoreCase = true;
- else if (flag == "-r" || flag == "--reverse") reverse = true;
- else if (flag == "-u" || flag == "--unique") unique = true;
- else if (flag == "-n" || flag == "--numeric-sort") numericSort = true;
- else if (flag == "-o" || Regex.Match(flag , @"--output=.*").Success)
- {
- fileOutput = true;
- if (flag == "-o")
- {
- outputFileName = flags[i + 1];
- i++;
- if (i == flags.Length - 1)
- {
- Console.WriteLine("Hmmm... Seems like you didn't specify the output file name.");
- return;
- }
- Console.WriteLine("Hmmm... Seems like you didn't specify the output file name.");
- return;
- }
- else
- {
- outputFileName = flag.Substring(9);
- if(outputFileName == "")
- {
- Console.Write("Hmmm... Seems like you didn't specify the file name.");
- return;
- }
- }
- }
- else
- {
- Console.WriteLine("Unknown flag: " + flag + ". Maybe try reading the documentation? (just an advise)");
- return;
- }
- }
- linesArray = SortLines(linesArray, reverse, unique, numericSort);
- Print(fileOutput, linesArray, outputFileName);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment