最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Array.Sort()静态方法对数组进行排序
时间:2011-08-19 编辑:简简单单 来源:一聚教程网
这个例子定义了一个int数组,然后使用Array.Sort(arr)静态方法对此数组进行排序,最后输出排序后的数组。以上例子将毫无意外的依次输出1,2,3,4,5,6.
int[] intArray = new int[]{2,3,6,1,4,5};
Array.Sort(intArray);
Array.ForEach(intArray,(i)=>Console.WriteLine(i));
排序是编程中常用的法算之一,排序的方法有很多种,下面介绍一种简单有效的排序方法,代码如下:
private bool isReverse = false;
private void Sort(PersonalNotificationEntity [] list,string key)
{
if ( isReverse )
{
Array.Reverse(list);
isReverse = false;
}
else
{
int len = list.Length;
Type type = typeof(PersonalNotificationEntity);
object [] keys = new object[len];
for(int i = 0 ; i < len ; i++)
{
keys[i] = type.InvokeMember(key,BindingFlags.GetField ,null,list[i],null);
}
Array.Sort(keys,list);
isReverse = true;
}
}
这里使用了Array.Sort()和Array.Reverse()方法对数据进行正/反排序,变量isReverse做为反排序的标志位
方法传入了2个参数,一个是要排序的对象数组list,一个是排序关键字key,即要对象的根据哪个属性或字段来进行排序(这个值是等于对象的属性/字段名)
type.InvokeMember()方法可以得到对象实例的属性/字段值
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10