public static void Sort(ref string[] lines, LineOptions lineOptions) { IComparer <string> comparer = new LineComparer(lineOptions); Array.Sort(lines, comparer); if (lineOptions.HasFlag(LineOptions.EliminateDuplicates)) { int numLines = lines.Length; List <string> newLines = new(numLines); string previousLine = null; for (int i = 0; i < numLines; i++) { string line = lines[i]; if (previousLine == null || comparer.Compare(line, previousLine) != 0) { newLines.Add(line); } previousLine = line; } lines = newLines.ToArray(); } }
public bool Execute(Options options) { LineOptions lineOptions = options.LineOptions; this.caseSensitive.IsChecked = lineOptions.HasFlag(LineOptions.CaseSensitive); this.compareByOrdinal.IsChecked = lineOptions.HasFlag(LineOptions.ByOrdinal); this.descending.IsChecked = lineOptions.HasFlag(LineOptions.Descending); this.ignoreWhitespace.IsChecked = lineOptions.HasFlag(LineOptions.IgnoreWhitespace); this.ignorePunctuation.IsChecked = lineOptions.HasFlag(LineOptions.IgnorePunctuation); this.wholeLines.IsChecked = lineOptions.HasFlag(LineOptions.WholeLines); this.eliminateDuplicates.IsChecked = lineOptions.HasFlag(LineOptions.EliminateDuplicates); this.compareByLength.IsChecked = lineOptions.HasFlag(LineOptions.ByLength); this.onlyShowWhenShiftIsPressed.IsChecked = options.OnlyShowSortLinesDialogWhenShiftIsPressed; bool result = false; if (this.ShowModal().GetValueOrDefault()) { lineOptions = LineOptions.None; void AddOption(CheckBox checkBox, LineOptions option) { if (checkBox.IsChecked.GetValueOrDefault()) { lineOptions |= option; } } AddOption(this.caseSensitive, LineOptions.CaseSensitive); AddOption(this.compareByOrdinal, LineOptions.ByOrdinal); AddOption(this.descending, LineOptions.Descending); AddOption(this.ignoreWhitespace, LineOptions.IgnoreWhitespace); AddOption(this.ignorePunctuation, LineOptions.IgnorePunctuation); AddOption(this.wholeLines, LineOptions.WholeLines); AddOption(this.eliminateDuplicates, LineOptions.EliminateDuplicates); AddOption(this.compareByLength, LineOptions.ByLength); options.LineOptions = lineOptions; options.OnlyShowSortLinesDialogWhenShiftIsPressed = this.onlyShowWhenShiftIsPressed.IsChecked.GetValueOrDefault(); options.SaveSettingsToStorage(); result = true; } return(result); }
private LineComparer(LineOptions options) { bool caseSensitive = options.HasFlag(LineOptions.CaseSensitive); if (options.HasFlag(LineOptions.ByOrdinal)) { this.comparison = caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; } else { this.comparison = caseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; } this.descending = options.HasFlag(LineOptions.Descending); this.ignoreWhitespace = options.HasFlag(LineOptions.IgnoreWhitespace); this.ignorePunctuation = options.HasFlag(LineOptions.IgnorePunctuation); this.byLength = options.HasFlag(LineOptions.ByLength); }