Skip to content

Commit

Permalink
Improve sorting by asset names
Browse files Browse the repository at this point in the history
- added alphanumeric sorting for more natural presentation of asset list
  • Loading branch information
aelurum committed Nov 24, 2021
1 parent 19c6c5f commit 9cbe91d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
10 changes: 10 additions & 0 deletions AssetStudioGUI/AssetStudioGUIForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ partial class AssetStudioGUIForm : Form
//asset list sorting
private int sortColumn = -1;
private bool reverseSort;
private AlphanumComparatorFast alphanumComparator = new AlphanumComparatorFast();

//asset list filter
private System.Timers.Timer delayTimer;
Expand Down Expand Up @@ -621,6 +622,15 @@ private void assetListView_ColumnClick(object sender, ColumnClickEventArgs e)
return reverseSort ? pathID_Y.CompareTo(pathID_X) : pathID_X.CompareTo(pathID_Y);
});
}
else if (sortColumn == 0) // Name
{
visibleAssets.Sort((a, b) =>
{
var at = a.SubItems[sortColumn].Text;
var bt = b.SubItems[sortColumn].Text;
return reverseSort ? alphanumComparator.Compare(bt, at) : alphanumComparator.Compare(at, bt);
});
}
else
{
visibleAssets.Sort((a, b) =>
Expand Down
96 changes: 96 additions & 0 deletions AssetStudioGUI/Components/AlphanumComparatorFast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// This code developed by Dot Net Perls
using System.Collections;


namespace AssetStudioGUI
{
internal class AlphanumComparatorFast : IComparer
{
public int Compare(object x, object y)
{
if (!(x is string s1))
{
return 0;
}
if (!(y is string s2))
{
return 0;
}

int len1 = s1.Length;
int len2 = s2.Length;
int marker1 = 0;
int marker2 = 0;

// Walk through two the strings with two markers.
while (marker1 < len1 && marker2 < len2)
{
char ch1 = s1[marker1];
char ch2 = s2[marker2];

// Some buffers we can build up characters in for each chunk.
char[] space1 = new char[len1];
int loc1 = 0;
char[] space2 = new char[len2];
int loc2 = 0;

// Walk through all following characters that are digits or
// characters in BOTH strings starting at the appropriate marker.
// Collect char arrays.
do
{
space1[loc1++] = ch1;
marker1++;

if (marker1 < len1)
{
ch1 = s1[marker1];
}
else
{
break;
}
} while (char.IsDigit(ch1) == char.IsDigit(space1[0]));

do
{
space2[loc2++] = ch2;
marker2++;

if (marker2 < len2)
{
ch2 = s2[marker2];
}
else
{
break;
}
} while (char.IsDigit(ch2) == char.IsDigit(space2[0]));

// If we have collected numbers, compare them numerically.
// Otherwise, if we have strings, compare them alphabetically.
string str1 = new string(space1);
string str2 = new string(space2);

int result;

if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
{
int thisNumericChunk = int.Parse(str1);
int thatNumericChunk = int.Parse(str2);
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
}

if (result != 0)
{
return result;
}
}
return len1 - len2;
}
}
}

0 comments on commit 9cbe91d

Please sign in to comment.