使用 IComparable 对对象进行排序
本文关键字:排序 对象 IComparable 使用 | 更新日期: 2023-09-27 17:56:59
我正在尝试在我的自定义对象中实现IComparable
接口,以便List.Sort()
可以按字母顺序对它们进行排序。
我的对象有一个名为 _name
的字段,它是一个字符串类型,我希望它基于此进行排序。这是我实现的方法:
public int CompareTo(object obj)
{
//Int reference table:
//1 or greater means the current instance occurs after obj
//0 means both elements occur in the same position
//-1 or less means the current instance occurs before obj
if (obj == null)
return 1;
Upgrade otherUpgrade = obj as Upgrade;
if (otherUpgrade != null)
return _name.CompareTo(otherUpgrade.Name);
else
throw new ArgumentException("Passed object is not an Upgrade.");
}
不确定我是否做错了什么,或者这只是字符串CompareTo
的工作方式,但基本上我的列表是这样排序的:
- 测试升级
- 测试升级 10
- 测试升级 11
- 测试升级 12
- 测试升级 13
- 测试升级 14
- 测试升级 15
- 测试升级 2
- 测试升级 3
- 测试升级 4
- 测试升级 5
我希望它们像这样排序:
- 测试升级
- 测试升级 2
- 测试升级 3
- 。等
你想要"自然顺序"——熟悉英语惯例的人会选择的排序规则——而不是你所拥有的,即"词典"排序:为每个字母分配严格的排序,然后依次按每个字母排序。
Jeff有一篇关于这里一些细节的好文章,其中包含指向尝试解决问题的不同算法的链接:
http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
Raymond 在这里讨论了 Windows 如何处理它:
http://technet.microsoft.com/en-us/magazine/hh475812.aspx
基本上问题是:自然顺序整理需要解决人工智能问题;你试图模仿人类会做什么,这可能令人惊讶地棘手。祝你好运!
字符串按字典顺序排序。 您必须将所有数字格式化为具有相同的长度(例如:测试升级 02),或者解析比较器中的数字并将其合并到比较逻辑中。
发生这种情况的原因是您正在进行字符串比较,它没有明确的数字知识。它按每个字符的相应字符代码对每个字符串进行排序。
要获得您想要的效果,需要更多的工作。请参阅此问题:对可能包含数字的字符串进行排序
字母数字排序
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;
}
}
用法:
using System;
using System.Collections;
class Program
{
static void Main()
{
string[] highways = new string[]
{
"100F",
"50F",
"SR100",
"SR9"
};
//
// We want to sort a string array called highways in an
// alphanumeric way. Call the static Array.Sort method.
//
Array.Sort(highways, new AlphanumComparatorFast());
//
// Display the results
//
foreach (string h in highways)
{
Console.WriteLine(h);
}
}
}
输出
50F 100F
SR9
SR100
感谢所有的回复。我做了自己的方法,似乎工作正常。它不适用于所有情况,但它适用于我的方案。以下是任何感兴趣的人的代码:
/// <summary>
/// Compares the upgrade to another upgrade
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int CompareTo(object obj)
{
//Int reference table:
//1 or greater means the current instance occurs after obj
//0 means both elements occur in the same position
//-1 or less means the current instance occurs before obj
if (obj == null)
return 1;
Upgrade otherUpgrade = obj as Upgrade;
if (otherUpgrade != null)
{
//Split strings into arrays
string[] splitStringOne = _name.Split(new char[] { ' ' });
string[] splitStringTwo = otherUpgrade.Name.Split(new char[] { ' ' });
//Will hold checks to see which comparer will be used
bool sameWords = false, sameLength = false, bothInt = false;
//Will hold the last part of the string if it is an int
int intOne = 0, intTwo = 0;
//Check if they have the same length
sameLength = (splitStringOne.Length == splitStringTwo.Length);
if (sameLength)
{
//Check to see if they both end in an int
bothInt = (int.TryParse(splitStringOne[splitStringOne.Length - 1], out intOne) && int.TryParse(splitStringTwo[splitStringTwo.Length - 1], out intTwo));
if (bothInt)
{
//Check to see if the previous parts of the string are equal
for (int i = 0; i < splitStringOne.Length - 2; i++)
{
sameWords = (splitStringOne[i].ToLower().Equals(splitStringTwo[i].ToLower()));
if (!sameWords)
break;
}
}
}
//If all criteria is met, use the customk comparer
if (sameWords && sameLength && bothInt)
{
if (intOne < intTwo)
return -1;
else if (intOne > intTwo)
return 1;
else //Both equal
return 0;
}
//Else use the default string comparer
else
return _name.CompareTo(otherUpgrade.Name);
}
else
throw new ArgumentException("Passed object is not an Upgrade.");
}
适用于使用" "字符间隔的字符串,如下所示:
测试数据:
- 你好 11
- 你好2
- 你好 13
结果
- 你好2
- 你好 11
- 你好 13
不适用于Hello11
和Hello2
等数据,因为它无法拆分它们。不区分大小写。