C#中List的排序(Sort)

要对自定义类数组或List进行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。

代码如下:

 

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif    
/// 继承IComparer<T>接口,实现同一自定义类型 对象比较
InBlock.gif    
/// </summary>
ExpandedBlockEnd.gif    
/// <typeparam name="T">T为泛用类型</typeparam>

None.gif      public   class  Reverser < T >  : IComparer < T >
ExpandedBlockStart.gifContractedBlock.gif    
... {
InBlock.gif        
private Type type = null;
InBlock.gif        
private ReverserInfo info;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="type">进行比较的类类型</param>
InBlock.gif        
/// <param name="name">进行比较对象的属性名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="direction">比较方向(升序/降序)</param>

InBlock.gif        public Reverser(Type type, string name, ReverserInfo.Direction direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
this.type = type;
InBlock.gif            
this.info.name = name;
InBlock.gif            
if (direction != ReverserInfo.Direction.ASC)
InBlock.gif                
this.info.direction = direction;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="className">进行比较的类名称</param>
InBlock.gif        
/// <param name="name">进行比较对象的属性名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="direction">比较方向(升序/降序)</param>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Reverser(string className, string name, ReverserInfo.Direction direction) ...{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
this.type = Type.GetType(className, true);
InBlock.gif                
this.info.name = name;
InBlock.gif                
this.info.direction = direction;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch (Exception e)...{
InBlock.gif                
throw new Exception(e.Message);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 构造函数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="t">进行比较的类型的实例</param>
InBlock.gif        
/// <param name="name">进行比较对象的属性名称</param>
ExpandedSubBlockEnd.gif        
/// <param name="direction">比较方向(升序/降序)</param>

InBlock.gif        public Reverser(T t, string name, ReverserInfo.Direction direction)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
this.type = t.GetType();
InBlock.gif            
this.info.name = name;
InBlock.gif            
this.info.direction = direction;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//必须!实现IComparer<T>的比较方法。
InBlock.gif
        int IComparer<T>.Compare(T t1, T t2)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
InBlock.gif            
object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
InBlock.gif            
if (this.info.direction != ReverserInfo.Direction.ASC)
InBlock.gif                Swap(
ref x, ref y);
InBlock.gif            
return (new CaseInsensitiveComparer()).Compare(x, y);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//交换操作数
InBlock.gif
        private void Swap(ref object x, ref object y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
object temp = null;
InBlock.gif            temp 
= x;
InBlock.gif            x 
= y;
InBlock.gif            y 
= temp;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif    
/// 对象比较时使用的信息类
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   struct  ReverserInfo
ExpandedBlockStart.gifContractedBlock.gif    
... {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 比较的方向,如下:
InBlock.gif        
/// ASC:升序
InBlock.gif        
/// DESC:降序
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public enum Direction
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            ASC 
= 0,
InBlock.gif            DESC,
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        
public enum Target
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            CUSTOMER 
= 0,
InBlock.gif            FORM,
InBlock.gif            FIELD,
InBlock.gif            SERVER,
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        
public string name;
InBlock.gif        
public Direction direction;
InBlock.gif        
public Target target;
ExpandedBlockEnd.gif    }

None.gif

上面主要是运用了 C#的反射 和 Framework中的排序算法。

像上面那样实现接口后,就可以使用List<T>进行 升序/降序 排序了。

测试代码如下:

 

None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Collections;
None.gif
using  System.Reflection;
None.gif
using  System.Text;
None.gif
None.gif
namespace  List_T_SortTest_u_2
ExpandedBlockStart.gifContractedBlock.gif
... {
ContractedSubBlock.gifExpandedSubBlockStart.gif    
测试Reverser代码段#region 测试Reverser<T>代码段
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 实体类User,测试用
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif        
protected string _name;
InBlock.gif        
protected int _age;
InBlock.gif        
protected string _address;
InBlock.gif
InBlock.gif        
public User(string name, int age, string address)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
this._name = name;
InBlock.gif            
this._age = age;
InBlock.gif            
this._address = address;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get ...return _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set ...{ _name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get ...return _age; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set ...{ _age = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string Address
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get ...return _address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set ...{ _address = value; }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 主程序类(启动类),测试用
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
...{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            List
<User> userList = new List<User>();
InBlock.gif            User user;
InBlock.gif
InBlock.gif            user 
= new User("Wang"21"ShenYang");
InBlock.gif            userList.Add(user);
InBlock.gif            user 
= new User("Yan"27"JinZhou");
InBlock.gif            userList.Add(user);
InBlock.gif            user 
= new User("Liu"26"BeiJing");
InBlock.gif            userList.Add(user);
InBlock.gif            user 
= new User("Zhao"30"ChaoYang");
InBlock.gif            userList.Add(user);
InBlock.gif            user 
= new User("Yang"27"FuXin");
InBlock.gif            userList.Add(user);
InBlock.gif
InBlock.gif            
//for (int i = 0; i < ar.Count; i++ )
InBlock.gif            
//    ;
InBlock.gif
            Console.Write("Name     ");
InBlock.gif            Console.Write(
"Age      ");
InBlock.gif            Console.Write(
"Address  " + " " + " ");
InBlock.gif            Console.WriteLine(
"-----------------------");
InBlock.gif            
foreach (User u in userList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                Console.Write(u.Name 
+ "    ");
InBlock.gif                Console.Write(u.Age 
+ "    ");
InBlock.gif                Console.Write(u.Address 
+ "    " + " ");
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine();
InBlock.gif
InBlock.gif            Reverser
<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC);
InBlock.gif            userList.Sort(reverser);
InBlock.gif            Console.WriteLine();
InBlock.gif            
foreach (User u in userList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                Console.Write(u.Name 
+ "    ");
InBlock.gif                Console.Write(u.Age 
+ "    ");
InBlock.gif                Console.Write(u.Address 
+ "    " + " ");
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.WriteLine();
InBlock.gif
InBlock.gif            reverser 
= new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC);
InBlock.gif            userList.Sort(reverser);
InBlock.gif            Console.WriteLine();
InBlock.gif            
foreach (User u in userList)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                Console.Write(u.Name 
+ "    ");
InBlock.gif                Console.Write(u.Age 
+ "    ");
InBlock.gif                Console.Write(u.Address 
+ "    " + " ");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            Console.Read();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值