最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Array.Sort()静态方法对数组进行排序
时间:2022-06-25 04:06:35 编辑:袖梨 来源:一聚教程网
这个例子定义了一个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()方法可以得到对象实例的属性/字段值
相关文章
- 原神振晶的应用研究第一天怎么过 原神气体流向的偏转攻略 04-28
- 王者荣耀一元通行证活动怎么玩 王者荣耀1元战令活动介绍 04-28
- 崩坏星穹铁道波提欧光锥推荐 崩坏星穹铁道波提欧带什么光锥 04-28
- 崩坏星穹铁道波提欧遗器推荐 崩坏星穹铁道波提欧遗器怎么搭配 04-28
- 崩坏星穹铁道波提欧遗器推荐 04-28
- 王者荣耀一元通行证活动怎么玩 04-28