summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Scene/NaturalSortComparer.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-25 14:28:30 +0800
committerchai <chaifix@163.com>2021-01-25 14:28:30 +0800
commit6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch)
tree7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/Scene/NaturalSortComparer.cs
+scripts
Diffstat (limited to 'Client/Assets/Scripts/Scene/NaturalSortComparer.cs')
-rw-r--r--Client/Assets/Scripts/Scene/NaturalSortComparer.cs89
1 files changed, 89 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/Scene/NaturalSortComparer.cs b/Client/Assets/Scripts/Scene/NaturalSortComparer.cs
new file mode 100644
index 00000000..e36011a0
--- /dev/null
+++ b/Client/Assets/Scripts/Scene/NaturalSortComparer.cs
@@ -0,0 +1,89 @@
+using System;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+
+// From http://zootfroot.blogspot.dk/2009/09/natural-sort-compare-with-linq-orderby.html
+public class NaturalSortComparer<T> : IComparer<string>, IDisposable
+{
+ private readonly bool isAscending;
+
+ public NaturalSortComparer(bool inAscendingOrder = true)
+ {
+ this.isAscending = inAscendingOrder;
+ }
+
+ #region IComparer<string> Members
+ public int Compare(string x, string y)
+ {
+ throw new NotImplementedException();
+ }
+ #endregion
+
+ #region IComparer<string> Members
+ int IComparer<string>.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);
+ }
+
+ int returnVal;
+
+ for (int i = 0; i < x1.Length && i < y1.Length; i++)
+ {
+ if (x1[i] != y1[i])
+ {
+ returnVal = PartCompare(x1[i], y1[i]);
+ return isAscending ? returnVal : -returnVal;
+ }
+ }
+
+ if (y1.Length > x1.Length)
+ {
+ returnVal = 1;
+ }
+ else if (x1.Length > y1.Length)
+ {
+ returnVal = -1;
+ }
+ else
+ {
+ returnVal = 0;
+ }
+
+ return isAscending ? returnVal : -returnVal;
+ }
+
+ 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);
+ }
+ #endregion
+
+ private Dictionary<string, string[]> table = new Dictionary<string, string[]>();
+
+ public void Dispose()
+ {
+ table.Clear();
+ table = null;
+ }
+} \ No newline at end of file