Sortieren einer generischen Liste auf der willkürlichen Eigenschaft alphanumerisch in C#
-
27-10-2019 - |
Frage
Ist es möglich, eine generische Liste auf willkürlicher Eigenschaft alphanumerisch in C#zu sortieren? Lassen Sie mich wissen, ob die Frage nicht klar ist und ich ein Beispiel finden werde.
Danke im Voraus
Hinweis: Ich habe diesen Link gefunden, der es tut, aber nicht alphanumerisch. Kann mir jemand helfen?http://blog.codewrench.net/2009/04/14/sorting-a-generic-list-on-arbitrary-property/
Lösung
Hier ist eine schnelle alphanumerische Sortierung (kann auch für andere Sorten mit Numerikern verwendet werden).
C# Alphanumerische Sortierung http://www.dotnetperls.com/alphanumeric-sorting
public class AlphanumComparatorFast : IComparer
{
public int Compare(object x, object y)
{
string s1 = x as string;
if (s1 == null)
{
return 0;
}
string s2 = y as string;
if (s2 == null)
{
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;
}
}
Verwendungszweck
var unordered = new[] { "100F", "50F", "SR100", "SR9" };
var ordered = unordered.OrderBy(s => s, new AlphanumComparatorFast());
Und hier ist ein schöner Artikel über das Problem:
Sortieren für Menschen: natürliche Sortierreihenfolge http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
Andere Tipps
Unter der Annahme, dass Sie mit "alphanumerisch" eine solche Sortierung meinen:
01a
02a
02b
0a
1a
1b
a
b
c
...
Sie könnten die verwenden OrderBY
Methode von Linq:
IEnumerable<T> myObjects = myObjects.OrderBy(x => x.SomeProperty);
Basierend auf PO -Eingängen bearbeiten:
Leider funktioniert das nicht einfach so, wie Sie es wollen (100F 50F SR100 SR9
geht zu 50F 100F SR9 SR100
). Aber Sie könnten das überlastete verwenden OrderBy
und implementieren Sie Ihre gewünschten IComparer
.