com.wallstop-studios.unity-helpers 2.0.0-rc76.5 → 2.0.0-rc76.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 55327b3f69ee4fde80aba6acb4e233b2
3
+ timeCreated: 1748619699
@@ -110,14 +110,20 @@
110
110
  switch (inputList)
111
111
  {
112
112
  case T[] array:
113
+ {
113
114
  Array.Sort(array, UnityObjectNameComparer<T>.Instance);
114
115
  return;
116
+ }
115
117
  case List<T> list:
118
+ {
116
119
  list.Sort(UnityObjectNameComparer<T>.Instance);
117
120
  return;
121
+ }
118
122
  default:
123
+ {
119
124
  inputList.InsertionSort(UnityObjectNameComparer<T>.Instance);
120
125
  break;
126
+ }
121
127
  }
122
128
  }
123
129
 
@@ -2,6 +2,7 @@
2
2
  {
3
3
  using System;
4
4
  using System.Collections.Generic;
5
+ using System.Text.RegularExpressions;
5
6
  #if UNITY_EDITOR
6
7
  using UnityEditor;
7
8
  #endif
@@ -9,6 +10,10 @@
9
10
  public sealed class UnityObjectNameComparer<T> : IComparer<T>
10
11
  where T : UnityEngine.Object
11
12
  {
13
+ private static readonly Regex TrailingNumberRegex = new(
14
+ @"^(.*?)(\d+)$",
15
+ RegexOptions.Compiled
16
+ );
12
17
  public static readonly UnityObjectNameComparer<T> Instance = new();
13
18
 
14
19
  private UnityObjectNameComparer() { }
@@ -30,7 +35,7 @@
30
35
  return -1;
31
36
  }
32
37
 
33
- int comparison = string.Compare(x.name, y.name, StringComparison.OrdinalIgnoreCase);
38
+ int comparison = CompareNatural(x.name, y.name);
34
39
  if (comparison != 0)
35
40
  {
36
41
  return comparison;
@@ -50,5 +55,45 @@
50
55
 
51
56
  return comparison;
52
57
  }
58
+
59
+ private static int CompareNatural(string nameA, string nameB)
60
+ {
61
+ Match mA = TrailingNumberRegex.Match(nameA);
62
+ Match mB = TrailingNumberRegex.Match(nameB);
63
+
64
+ bool hasNumberA = mA.Success;
65
+ bool hasNumberB = mB.Success;
66
+
67
+ // If both have trailing numbers, compare prefix then numeric
68
+ if (hasNumberA && hasNumberB)
69
+ {
70
+ string prefixA = mA.Groups[1].Value;
71
+ string prefixB = mB.Groups[1].Value;
72
+
73
+ int prefixCompare = StringComparer.OrdinalIgnoreCase.Compare(prefixA, prefixB);
74
+ if (prefixCompare != 0)
75
+ {
76
+ return prefixCompare;
77
+ }
78
+
79
+ // same prefix → compare parsed integers
80
+ int numA = int.Parse(mA.Groups[2].Value);
81
+ int numB = int.Parse(mB.Groups[2].Value);
82
+ return numA.CompareTo(numB);
83
+ }
84
+ // If only one has a trailing number, treat the one without number as coming first
85
+
86
+ if (hasNumberA)
87
+ {
88
+ return 1; // B (no number) comes before A
89
+ }
90
+
91
+ if (hasNumberB)
92
+ {
93
+ return -1; // A (no number) comes before B
94
+ }
95
+ // Neither has a trailing number → pure string compare
96
+ return StringComparer.OrdinalIgnoreCase.Compare(nameA, nameB);
97
+ }
53
98
  }
54
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.wallstop-studios.unity-helpers",
3
- "version": "2.0.0-rc76.5",
3
+ "version": "2.0.0-rc76.6",
4
4
  "displayName": "Unity Helpers",
5
5
  "description": "Various Unity Helper Library",
6
6
  "dependencies": {},
@@ -79,5 +79,6 @@
79
79
 
80
80
 
81
81
 
82
+
82
83
 
83
84