Skip to content

Commit 76a7a22

Browse files
committed
feature: use numeric sorting for all trees (#597)
Signed-off-by: leo <[email protected]>
1 parent 12bb915 commit 76a7a22

File tree

4 files changed

+78
-6
lines changed

4 files changed

+78
-6
lines changed

src/Models/NumericSort.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
namespace SourceGit.Models
2+
{
3+
public static class NumericSort
4+
{
5+
public static int Compare(string s1, string s2)
6+
{
7+
int len1 = s1.Length;
8+
int len2 = s2.Length;
9+
10+
int marker1 = 0;
11+
int marker2 = 0;
12+
13+
while (marker1 < len1 && marker2 < len2)
14+
{
15+
char c1 = s1[marker1];
16+
char c2 = s2[marker2];
17+
18+
char[] space1 = new char[len1];
19+
char[] space2 = new char[len2];
20+
21+
int loc1 = 0;
22+
int loc2 = 0;
23+
24+
bool isDigit1 = char.IsDigit(c1);
25+
do
26+
{
27+
space1[loc1] = c1;
28+
loc1++;
29+
marker1++;
30+
31+
if (marker1 < len1)
32+
c1 = s1[marker1];
33+
else
34+
break;
35+
} while (char.IsDigit(c1) == isDigit1);
36+
37+
bool isDigit2 = char.IsDigit(c2);
38+
do
39+
{
40+
space2[loc2] = c2;
41+
loc2++;
42+
marker2++;
43+
44+
if (marker2 < len2)
45+
c2 = s2[marker2];
46+
else
47+
break;
48+
} while (char.IsDigit(c2) == isDigit2);
49+
50+
string sub1 = new string(space1, 0, loc1);
51+
string sub2 = new string(space2, 0, loc2);
52+
53+
int result;
54+
if (isDigit1 && isDigit2)
55+
{
56+
int num1 = int.Parse(sub1);
57+
int num2 = int.Parse(sub2);
58+
result = num1 - num2;
59+
}
60+
else
61+
{
62+
result = string.Compare(sub1, sub2, System.StringComparison.Ordinal);
63+
}
64+
65+
if (result != 0)
66+
return result;
67+
}
68+
69+
return len1 - len2;
70+
}
71+
}
72+
}

src/ViewModels/BranchTreeNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ private void SortNodes(List<BranchTreeNode> nodes)
194194
return -1;
195195

196196
if (l.Backend is Models.Branch)
197-
return r.Backend is Models.Branch ? string.Compare(l.Name, r.Name, StringComparison.Ordinal) : 1;
197+
return r.Backend is Models.Branch ? Models.NumericSort.Compare(l.Name, r.Name) : 1;
198198

199-
return r.Backend is Models.Branch ? -1 : string.Compare(l.Name, r.Name, StringComparison.Ordinal);
199+
return r.Backend is Models.Branch ? -1 : Models.NumericSort.Compare(l.Name, r.Name);
200200
});
201201

202202
foreach (var node in nodes)

src/ViewModels/ChangeTreeNode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ private static void Sort(List<ChangeTreeNode> nodes)
114114

115115
nodes.Sort((l, r) =>
116116
{
117-
if (l.IsFolder)
118-
return r.IsFolder ? string.Compare(l.FullPath, r.FullPath, StringComparison.Ordinal) : -1;
119-
return r.IsFolder ? 1 : string.Compare(l.FullPath, r.FullPath, StringComparison.Ordinal);
117+
if (l.IsFolder == r.IsFolder)
118+
return Models.NumericSort.Compare(l.FullPath, r.FullPath);
119+
return r.IsFolder ? -1 : 1;
120120
});
121121
}
122122

src/Views/RevisionFileTreeView.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private void OnRowsSelectionChanged(object sender, SelectionChangedEventArgs _)
284284
node.Children.Sort((l, r) =>
285285
{
286286
if (l.IsFolder == r.IsFolder)
287-
return string.Compare(l.Name, r.Name, StringComparison.Ordinal);
287+
return Models.NumericSort.Compare(l.Name, r.Name);
288288
return l.IsFolder ? -1 : 1;
289289
});
290290

0 commit comments

Comments
 (0)