Array.Sort<T>(T[] array, Comparison<T> comparison)只要您提供正确的比较委托,就不限于任何类型.比较委托应该返回包含比较操作结果的整数值,而不是要与某些东西进行比较的值.例如,如果您有X包含float属性的类,则Property可以按如下方式对它们进行排序:
public sealed class X
{
public float Property { get; set; }
}
static void Test()
{
var arr = new X[]
{
new X { Property = 5.5f },
new X { Property = 2.5f },
new X { Property = 6.5f },
new X { Property = 1.2f },
};
Array.Sort(arr, (a, b) => a.Property.CompareTo(b.Property));
}
Run Code Online (Sandbox Code Playgroud)
您可以使用LINQ OrderBy扩展方法
var arr = new[] { 1.3f, 1.4f };
arr.OrderBy( a => a ).ToArray();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7998 次 |
| 最近记录: |