Improve sorting performance

Improved performance of AlphanumComparatorFast sorting for names, as well as the default sorting for containers
This commit is contained in:
VaDiM
2022-12-03 04:47:42 +03:00
parent 6a9aad510c
commit dfbe46e1e5
3 changed files with 112 additions and 22 deletions

View File

@ -1,22 +1,16 @@
// This code developed by Dot Net Perls
using System.Collections;
// AlphanumComparatorFast mod by VaDiM
// Original code was developed by Dot Net Perls
// For more detail visit: https://www.dotnetperls.com/alphanumeric-sorting
using System;
using System.Collections.Generic;
namespace AssetStudioGUI
{
internal class AlphanumComparatorFast : IComparer
internal class AlphanumComparatorFast : IComparer<string>
{
public int Compare(object x, object y)
public int Compare(string s1, string s2)
{
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;
@ -69,20 +63,17 @@ namespace AssetStudioGUI
// 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);
int thisNumericChunk = int.Parse(new string(space1));
int thatNumericChunk = int.Parse(new string(space2));
result = thisNumericChunk.CompareTo(thatNumericChunk);
}
else
{
result = str1.CompareTo(str2);
result = MemoryExtensions.CompareTo(space1, space2, StringComparison.OrdinalIgnoreCase);
}
if (result != 0)