public override int Compare(string x, string y) { if (string.IsNullOrEmpty(x) || string.IsNullOrEmpty(y) || x == y) { return(0); } string[] strArray1; if (!this.splitTable.TryGetValue(x, out strArray1)) { strArray1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); this.splitTable.Add(x, strArray1); } string[] strArray2; if (!this.splitTable.TryGetValue(y, out strArray2)) { strArray2 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); this.splitTable.Add(y, strArray2); } for (int index = 0; index < strArray1.Length && index < strArray2.Length; ++index) { int num = NaturalSortComparer.PartCompare(strArray1[index], strArray2[index]); if (num != 0) { return(num); } } return(strArray1.Length == strArray2.Length ? x.Length.CompareTo(y.Length) : strArray1.Length.CompareTo(strArray2.Length)); }
/// <summary> /// Отправляет список объектов учёта. /// </summary> /// <param name="user"></param> /// <param name="arguments"></param> /// <returns></returns> private async System.Threading.Tasks.Task ShowNodes(User user, string[] arguments) { if (user.Context == null) { throw new UnauthorizedCommandException(GetNodesCommand); } var client = new Lers.Rest.NodesClient(user.Context.BaseUri.ToString(), user.Context.RestClient); var nodes = await client.GetNodes(arguments); long chatId = user.ChatId; if (!nodes.Any()) { await _bot.SendText(chatId, "Ничего не найдено"); return; } var comparer = new NaturalSortComparer(); await SendListMessage(chatId, nodes.OrderBy(x => x.Title, comparer), x => x.Title); }
public void NaturalSortComparerTest() { // Arrange var comparer = new NaturalSortComparer(); var source = new[] { "2name", "1name", "20name", "3name", "11name" }; // Act string[] destination = source.OrderBy(p => p, comparer).ToArray(); // Assert Assert.AreEqual(source.Length, destination.Length); Assert.AreEqual("1name", destination[0]); Assert.AreEqual("2name", destination[1]); Assert.AreEqual("3name", destination[2]); Assert.AreEqual("11name", destination[3]); Assert.AreEqual("20name", destination[4]); }
int IComparer <string> .Compare(string x, string y) { if (x == y) { return(0); } string[] array; if (!this.table.TryGetValue(x, out array)) { array = Regex.Split(x.Replace(" ", string.Empty), "([0-9]+)"); this.table.Add(x, array); } string[] array2; if (!this.table.TryGetValue(y, out array2)) { array2 = Regex.Split(y.Replace(" ", string.Empty), "([0-9]+)"); this.table.Add(y, array2); } int num = 0; int num2; while (num < array.Length && num < array2.Length) { if (array[num] != array2[num]) { num2 = NaturalSortComparer <T> .PartCompare(array[num], array2[num]); return((!this.isAscending) ? (-num2) : num2); } num++; } if (array2.Length > array.Length) { num2 = 1; } else if (array.Length > array2.Length) { num2 = -1; } else { num2 = 0; } return((!this.isAscending) ? (-num2) : num2); }
/// <summary> /// Returns a list of all the seed script file paths sorted by name. /// </summary> public IEnumerable<string> GetScripts(string setName) { IComparer<string> comparer = new NaturalSortComparer<string>(); string seedPath = GetPath(null); IEnumerable<string> seedScripts = Directory.GetFiles(seedPath, ScriptFileNamePattern).OrderBy(x => x, comparer); if (!string.IsNullOrEmpty(setName)) { string setPath = Path.Combine(seedPath, setName); if (Directory.Exists(setPath)) { seedScripts = seedScripts.Union(Directory.GetFiles(setPath, ScriptFileNamePattern).OrderBy(x => x, comparer)); } } return seedScripts; }
private void RefreshListViews() { var cmp = new NaturalSortComparer(true); UnassignedEntries.Sort((a, b) => cmp.Compare(a.Name, b.Name)); unassignedEntriesView.VirtualListSize = UnassignedEntries.Count; unassignedEntriesView.Refresh(); Groups.Sort(); entryGroupsView.VirtualListSize = Groups.ElementCount(); entryGroupsView.Refresh(); unassignedEntriesView.SelectedIndices.Clear(); entryGroupsView.SelectedIndices.Clear(); UpdateFileCounts(); }
/// <summary> /// Returns a list of all the seed script file paths sorted by name. /// </summary> public IEnumerable <string> GetScripts(string setName) { IComparer <string> comparer = new NaturalSortComparer <string>(); string seedPath = GetPath(null); IEnumerable <string> seedScripts = Directory.GetFiles(seedPath, ScriptFileNamePattern).OrderBy(x => x, comparer); if (!string.IsNullOrEmpty(setName)) { string setPath = Path.Combine(seedPath, setName); if (Directory.Exists(setPath)) { seedScripts = seedScripts.Union(Directory.GetFiles(setPath, ScriptFileNamePattern).OrderBy(x => x, comparer)); } } return(seedScripts); }
public void NaturalSortComparerPerformanceTest() { // Performance test bool ascending = true; bool output = false; int size = 1000000; TimeSpan naturalsorttime; #if USE_NATIVE_STRCMP TimeSpan NaturalStringComparerTime; #endif TimeSpan logicalsorttime; TimeSpan normalsorttime; Random rnd = new Random(DateTime.Now.Millisecond); List<string> testItems = new List<string>(size); for (int i = 0; i < size; i++) { if (i % 2 == 0) testItems.Add(rnd.Next(100).ToString() + ((char)('A' + rnd.Next(25))) + rnd.Next(100).ToString()); else testItems.Add(((char)('A' + rnd.Next(25))) + rnd.Next(100).ToString()); } List<string> testitems2 = new List<string>(testItems); List<string> testitems3 = new List<string>(testItems); #if USE_NATIVE_STRCMP List<string> testitems4 = new List<string>(testItems); #endif DateTime start = DateTime.Now; NaturalSortComparer comparer = new NaturalSortComparer(ascending); testItems.Sort(comparer); DateTime stop = DateTime.Now; naturalsorttime = stop - start; start = DateTime.Now; LogicalStringComparer logicalcomparer = new LogicalStringComparer(ascending); testitems2.Sort(logicalcomparer); stop = DateTime.Now; logicalsorttime = stop - start; start = DateTime.Now; testitems3.Sort(); stop = DateTime.Now; normalsorttime = stop - start; #if USE_NATIVE_STRCMP start = DateTime.Now; testitems4.Sort(new NaturalStringComparer(ascending)); stop = DateTime.Now; NaturalStringComparerTime = stop - start; #endif if (output) { Debug.WriteLine("Natural Comparison: "); foreach (string _item in testItems) { Debug.WriteLine(_item); } Debug.WriteLine("Logical Comparison: "); foreach (string _item in testitems2) { Debug.WriteLine(_item); } Debug.WriteLine("Normal Comparison"); foreach (string _item in testitems3) { Debug.WriteLine(_item); } } Console.WriteLine("Elapsed time for natural sort: " + naturalsorttime); Console.WriteLine("Elapsed time for logical sort: " + logicalsorttime); #if USE_NATIVE_STRCMP Console.WriteLine("Elapsed time for native natural sort: " + NaturalStringComparerTime); #endif Console.WriteLine("Elapsed time for normal sort : " + normalsorttime); for (int i = 0; i < testItems.Count; i++) { Assert.AreEqual(testItems[i], testitems2[i]); #if USE_NATIVE_STRCMP Assert.AreEqual(testItems[i], testitems4[i]); #endif } }
/// <summary> /// Sorts list using Natural Compare i.e. 1 is before 12, etc /// </summary> /// <param name="list"></param> public static void NaturalSort(this List <string> list) { NaturalSortComparer comparer = new NaturalSortComparer(); list.Sort(comparer); }
public ManuallyLinkedFilesControl() { InitializeComponent(); ManuallyLinkedFiles = new ObservableCollection <VM_VideoLocal>(); ViewFiles = CollectionViewSource.GetDefaultView(ManuallyLinkedFiles); ((ListCollectionView)ViewFiles).CustomSort = Comparer <VM_VideoLocal> .Create((x, y) => NaturalSortComparer.CompareNatural(x.FullPath, y.FullPath)); ViewFiles.Filter = FileSearchFilter; btnClearSearch.Click += new RoutedEventHandler(btnClearSearch_Click); txtFileSearch.TextChanged += new TextChangedEventHandler(txtFileSearch_TextChanged); btnRefresh.Click += new RoutedEventHandler(btnRefresh_Click); btnRescan.Click += new RoutedEventHandler(btnRescan_Click); }
public void NaturalSortComparerPerformanceTest() { // Performance test bool ascending = true; bool output = false; int size = 1000000; TimeSpan naturalsorttime; #if USE_NATIVE_STRCMP TimeSpan NaturalStringComparerTime; #endif TimeSpan logicalsorttime; TimeSpan normalsorttime; Random rnd = new Random(DateTime.Now.Millisecond); List <string> testItems = new List <string>(size); for (int i = 0; i < size; i++) { if (i % 2 == 0) { testItems.Add(rnd.Next(100).ToString() + ((char)('A' + rnd.Next(25))) + rnd.Next(100).ToString()); } else { testItems.Add(((char)('A' + rnd.Next(25))) + rnd.Next(100).ToString()); } } List <string> testitems2 = new List <string>(testItems); List <string> testitems3 = new List <string>(testItems); #if USE_NATIVE_STRCMP List <string> testitems4 = new List <string>(testItems); #endif DateTime start = DateTime.Now; NaturalSortComparer comparer = new NaturalSortComparer(ascending); testItems.Sort(comparer); DateTime stop = DateTime.Now; naturalsorttime = stop - start; start = DateTime.Now; LogicalStringComparer logicalcomparer = new LogicalStringComparer(ascending); testitems2.Sort(logicalcomparer); stop = DateTime.Now; logicalsorttime = stop - start; start = DateTime.Now; testitems3.Sort(); stop = DateTime.Now; normalsorttime = stop - start; #if USE_NATIVE_STRCMP start = DateTime.Now; testitems4.Sort(new NaturalStringComparer(ascending)); stop = DateTime.Now; NaturalStringComparerTime = stop - start; #endif if (output) { Debug.WriteLine("Natural Comparison: "); foreach (string _item in testItems) { Debug.WriteLine(_item); } Debug.WriteLine("Logical Comparison: "); foreach (string _item in testitems2) { Debug.WriteLine(_item); } Debug.WriteLine("Normal Comparison"); foreach (string _item in testitems3) { Debug.WriteLine(_item); } } Console.WriteLine("Elapsed time for natural sort: " + naturalsorttime); Console.WriteLine("Elapsed time for logical sort: " + logicalsorttime); #if USE_NATIVE_STRCMP Console.WriteLine("Elapsed time for native natural sort: " + NaturalStringComparerTime); #endif Console.WriteLine("Elapsed time for normal sort : " + normalsorttime); for (int i = 0; i < testItems.Count; i++) { Assert.AreEqual(testItems[i], testitems2[i]); #if USE_NATIVE_STRCMP Assert.AreEqual(testItems[i], testitems4[i]); #endif } }
/// <summary> /// Initializes a new instance of the <see cref="UnityEngine.TextureSortComparer"/> class. /// </summary> /// <param name="inAscendingOrder">If set to <c>true</c> in ascending order.</param> public TextureSortComparer(bool inAscendingOrder) { nameComparer = new NaturalSortComparer(inAscendingOrder); }
/// <summary> /// Insert a book into the appropriate place. If there is already a book with the same FolderPath, replace it. /// </summary> /// <param name="bookInfo"></param> public void InsertBookInfo(BookInfo bookInfo) { IComparer<string> comparer = new NaturalSortComparer<string>(); for (int i = 0; i < _bookInfos.Count; i++) { var compare = comparer.Compare(_bookInfos[i].FolderPath, bookInfo.FolderPath); if (compare == 0) { _bookInfos[i] = bookInfo; // Replace return; } if (compare > 0) { _bookInfos.Insert(i, bookInfo); return; } } _bookInfos.Add(bookInfo); }