C#中List的排序(Sort)

简介: 要对自定义类数组或List进行排序,譬如: List userList; ArrayList arrayList; 最重要的是:继承IComparer接口,实现int IComparer.Compare(T t1, T t2)方法。

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

List<User> userList;

ArrayList arrayList;

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

代码如下:

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    
/**/ /// <summary>
img_33d02437d135341f0800e3d415312ae8.gif    
/// 继承IComparer<T>接口,实现同一自定义类型 对象比较
img_33d02437d135341f0800e3d415312ae8.gif    
/// </summary>
img_05dd8d549cff04457a6366b0a7c9352a.gif    
/// <typeparam name="T">T为泛用类型</typeparam>

img_a6339ee3e57d1d52bc7d02b338e15a60.gif      public   class  Reverser < T >  : IComparer < T >
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    
... {
img_33d02437d135341f0800e3d415312ae8.gif        
private Type type = null;
img_33d02437d135341f0800e3d415312ae8.gif        
private ReverserInfo info;
img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// 构造函数
img_33d02437d135341f0800e3d415312ae8.gif        
/// </summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// <param name="type">进行比较的类类型</param>
img_33d02437d135341f0800e3d415312ae8.gif        
/// <param name="name">进行比较对象的属性名称</param>
img_105a1e124122b2abcee4ea8e9f5108f3.gif        
/// <param name="direction">比较方向(升序/降序)</param>

img_33d02437d135341f0800e3d415312ae8.gif        public Reverser(Type type, string name, ReverserInfo.Direction direction)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            
this.type = type;
img_33d02437d135341f0800e3d415312ae8.gif            
this.info.name = name;
img_33d02437d135341f0800e3d415312ae8.gif            
if (direction != ReverserInfo.Direction.ASC)
img_33d02437d135341f0800e3d415312ae8.gif                
this.info.direction = direction;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

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

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        public Reverser(string className, string name, ReverserInfo.Direction direction) ...{
img_33d02437d135341f0800e3d415312ae8.gif            
try
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
...{
img_33d02437d135341f0800e3d415312ae8.gif                
this.type = Type.GetType(className, true);
img_33d02437d135341f0800e3d415312ae8.gif                
this.info.name = name;
img_33d02437d135341f0800e3d415312ae8.gif                
this.info.direction = direction;
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
catch (Exception e)...{
img_33d02437d135341f0800e3d415312ae8.gif                
throw new Exception(e.Message);
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

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

img_33d02437d135341f0800e3d415312ae8.gif        public Reverser(T t, string name, ReverserInfo.Direction direction)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            
this.type = t.GetType();
img_33d02437d135341f0800e3d415312ae8.gif            
this.info.name = name;
img_33d02437d135341f0800e3d415312ae8.gif            
this.info.direction = direction;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

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

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
//交换操作数
img_33d02437d135341f0800e3d415312ae8.gif
        private void Swap(ref object x, ref object y)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            
object temp = null;
img_33d02437d135341f0800e3d415312ae8.gif            temp 
= x;
img_33d02437d135341f0800e3d415312ae8.gif            x 
= y;
img_33d02437d135341f0800e3d415312ae8.gif            y 
= temp;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_05dd8d549cff04457a6366b0a7c9352a.gif    }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    
/**/ /// <summary>
img_33d02437d135341f0800e3d415312ae8.gif    
/// 对象比较时使用的信息类
img_05dd8d549cff04457a6366b0a7c9352a.gif    
/// </summary>

img_a6339ee3e57d1d52bc7d02b338e15a60.gif      public   struct  ReverserInfo
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    
... {
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif        
/// 比较的方向,如下:
img_33d02437d135341f0800e3d415312ae8.gif        
/// ASC:升序
img_33d02437d135341f0800e3d415312ae8.gif        
/// DESC:降序
img_105a1e124122b2abcee4ea8e9f5108f3.gif        
/// </summary>

img_33d02437d135341f0800e3d415312ae8.gif        public enum Direction
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            ASC 
= 0,
img_33d02437d135341f0800e3d415312ae8.gif            DESC,
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }
;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public enum Target
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            CUSTOMER 
= 0,
img_33d02437d135341f0800e3d415312ae8.gif            FORM,
img_33d02437d135341f0800e3d415312ae8.gif            FIELD,
img_33d02437d135341f0800e3d415312ae8.gif            SERVER,
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }
;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public string name;
img_33d02437d135341f0800e3d415312ae8.gif        
public Direction direction;
img_33d02437d135341f0800e3d415312ae8.gif        
public Target target;
img_05dd8d549cff04457a6366b0a7c9352a.gif    }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif

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

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

测试代码如下:

 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections.Generic;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Collections;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Reflection;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
using  System.Text;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
namespace  List_T_SortTest_u_2
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif
... {
img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif    
测试Reverser代码段#region 测试Reverser<T>代码段
img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif    
/// 实体类User,测试用
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
/// </summary>

img_33d02437d135341f0800e3d415312ae8.gif    public class User
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
...{
img_33d02437d135341f0800e3d415312ae8.gif        
protected string _name;
img_33d02437d135341f0800e3d415312ae8.gif        
protected int _age;
img_33d02437d135341f0800e3d415312ae8.gif        
protected string _address;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public User(string name, int age, string address)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_33d02437d135341f0800e3d415312ae8.gif            
this._name = name;
img_33d02437d135341f0800e3d415312ae8.gif            
this._age = age;
img_33d02437d135341f0800e3d415312ae8.gif            
this._address = address;
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public string Name
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
get ...return _name; }
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
set ...{ _name = value; }
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public int Age
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
get ...return _age; }
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
set ...{ _age = value; }
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif        
public string Address
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif        
...{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
get ...return _address; }
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            
set ...{ _address = value; }
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_33d02437d135341f0800e3d415312ae8.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif    
/**//// <summary>
img_33d02437d135341f0800e3d415312ae8.gif    
/// 主程序类(启动类),测试用
img_105a1e124122b2abcee4ea8e9f5108f3.gif    
/// </summary>

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

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

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

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif            Console.Read();
img_105a1e124122b2abcee4ea8e9f5108f3.gif        }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    
#endregion

img_33d02437d135341f0800e3d415312ae8.gif
img_05dd8d549cff04457a6366b0a7c9352a.gif}

img_a6339ee3e57d1d52bc7d02b338e15a60.gif

 

 

版权

作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

img_2c313bac282354945ea179a807d7e70d.jpg

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
8月前
|
开发框架 .NET C#
C#学习相关系列之Linq常用方法---排序(一)
C#学习相关系列之Linq常用方法---排序(一)
|
8月前
|
C#
C#中sort排序相关用法介绍
C#中sort排序相关用法介绍
|
C#
c#集合去重&排序常用方法
list和数组转Hashset跟SortedSet进行排序和去重,以及当Hashset和SortedSet存放的是类时如何进行自定义的排序和去重
107 2
|
消息中间件 存储 NoSQL
C#实现一个万物皆可排序的队列2
C#实现一个万物皆可排序的队列2
101 0
|
存储 安全 算法
C#实现一个万物皆可排序的队列1
C#实现一个万物皆可排序的队列1
100 0
|
C#
C#中汉字排序简单示例(拼音/笔划)
可以按照区域语言修改排序规则。 class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)", "錢(QIAN)", "孫(SU...
2525 0
|
SQL 存储 开发框架
C#——List排序
C#——List排序
168 0
C#编程-74:dataGridView排序和筛选
C#编程-74:dataGridView排序和筛选
407 0
|
算法 搜索推荐 C#
【愚公系列】2021年11月 C#版 数据结构与算法解析(二叉树排序)
【愚公系列】2021年11月 C#版 数据结构与算法解析(二叉树排序)
122 0
【愚公系列】2021年11月 C#版 数据结构与算法解析(二叉树排序)