Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
ZennoPoster Snippets
https://github.com/zennohelpers/snippets
Admin
π ΠΠΎΠ»Π»Π΅ΠΊΡΠΈΡ ΡΠ½ΠΈΠΏΠΏΠ΅ΡΠΎΠ² ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ ZennoPoster
Tokens:
900
Snippets:
2
Trust Score:
5.9
Update:
1 year ago
Context
Chat
Benchmark
85
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### 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<string>`, 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<string> sortList; // ΠΊΠΎΠΏΠΈΡΡΠ΅ΠΌ ΠΈΡΡ ΠΎΠ΄Π½ΡΠΉ ΡΠΏΠΈΠΎΡΠΊ Π² ΠΏΡΠΎΠΌΠ΅ΠΆΡΡΠΎΡΠ½ΡΠΉ sortList = new List<string>(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<string>` 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<string>, IDisposable { private Dictionary<string, string[]> table; public NaturalComparer() { table = new Dictionary<string, string[]>(); } 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); } } ```