### Using Natural Sorting with ZennoPoster Lists in C# Source: https://github.com/zennohelpers/snippets/blob/master/Сниппеты/[Списки]/Естественная сортировка списка.txt This C# code snippet demonstrates how to use the `NaturalComparer` class to sort a ZennoPoster list. It involves copying the source list to a temporary `List`, applying the natural sort using the `NaturalComparer`, and then adding the sorted elements to a destination ZennoPoster list. This is practical for organizing items in ZennoPoster that require human-friendly numerical ordering. ```csharp // Использование в кубике C# // исходный ZennoPoster список IZennoList notSortedZennoList = project.Lists["notSortedZennoList"]; // результирующий ZennoPoster список IZennoList sortedZennoList = project.Lists["sortedZennoList"]; // промежуточный список в который будет помещён результат List sortList; // копируем исходный спиоск в промежуточный sortList = new List(notSortedZennoList); // производим естественную сортировку using(NaturalComparer comparer = new NaturalComparer()) { sortList.Sort(comparer); } // сохраняем результат в результирующем списке sortedZennoList.AddRange(sortList); ``` -------------------------------- ### Natural Sorting Implementation in C# Source: https://github.com/zennohelpers/snippets/blob/master/Сниппеты/[Списки]/Естественная сортировка списка.txt This C# code defines a `NaturalComparer` class that implements the `Comparer` and `IDisposable` interfaces. It allows for natural sorting of strings, correctly handling numbers within the strings. The `Compare` method splits strings into parts and compares them, treating numeric parts as integers. This is useful for scenarios like sorting file names. ```csharp /* ЕСТЕСТВЕННАЯ СОРТИРОВКА СПИСКА Сниппет позволяет производить естественную сортировку списка, как в жизни: 1.2.3.4.5 итд. а не 1.10.11.12.2.20.21 итд. Полезно при работе с файлами, папками, которые нумеруются по порядку. */ // Данный код следует добавить в "Директивы using и общий код" public class NaturalComparer : Comparer, IDisposable { private Dictionary table; public NaturalComparer() { table = new Dictionary(); } public void Dispose() { table.Clear(); table = null; } public override int Compare(string x, string y) { if(x == y) { return 0; } string[] x1, y1; if(!table.TryGetValue(x, out x1)) { x1 = Regex.Split(x.Replace(" ", ""), "([0-9]+)"); table.Add(x, x1); } if(!table.TryGetValue(y, out y1)) { y1 = Regex.Split(y.Replace(" ", ""), "([0-9]+)"); table.Add(y, y1); } for(int i = 0; i < x1.Length && i < y1.Length; i++) { if(x1[i] != y1[i]) { return PartCompare(x1[i], y1[i]); } } if(y1.Length > x1.Length) { return 1; } else if(x1.Length > y1.Length) { return -1; } else { return 0; } } private static int PartCompare(string left, string right) { int x, y; if(!int.TryParse(left, out x)) { return left.CompareTo(right); } if(!int.TryParse(right, out y)) { return left.CompareTo(right); } return x.CompareTo(y); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.