对List排序

  泛型 List 实现泛型接口 IComparer 排序速度快,效率高,内部使用的排序算法为快速排序。 using System;
using System.Collections.Generic;

namespace ListSort
{
    
class Program
    {
        
static void Main( string [] args)
        {
            
// 初始化List<Obj>
            List < Obj > l = new List < Obj > ();
            l.Add(
new Obj( " 1 " , " s " ));
            l.Add(
new Obj( " 2 " , " x " ));
            l.Add(
new Obj( " 3 " , " h " ));
            l.Add(
new Obj( " 4 " , " y " ));
            l.Add(
new Obj( " 5 " , " j " ));
            l.Add(
new Obj( " 6 " , " r " ));
            l.Add(
new Obj( " 7 " , " c " ));
            l.Add(
new Obj( " 8 " , " d " ));

            
// ----------------------------------------------------------------------------

            Console.WriteLine(
" \n- - - 初 始 值 - - - " );

            
foreach (Obj o in l)
            {
                Console.WriteLine(
" {0} - - - - - {1} " , o.A, o.B);
            }

            Console.WriteLine(
" \n- - - - - - - - - - 美 丽 的 分 隔 符 - - - - - - - - - - " );


            Comparer c
= new Comparer();
            
// 排序
            l.Sort(c);

            
// ----------------------------------------------------------------------------

            Console.WriteLine(
" \n- - - 排 序 后 1 - - - " );

            
foreach (Obj o in l)
            {
                Console.WriteLine(
" {0} - - - - - {1} " , o.A, o.B);
            }

            Console.WriteLine(
" \n- - - - - - - - - - 美 丽 的 分 隔 符 - - - - - - - - - - " );

            
// 再次初始化List<Obj>
            l.Clear();

            l.Add(
new Obj( " 1 " , " s " ));
            l.Add(
new Obj( " 2 " , " x " ));
            l.Add(
new Obj( " 3 " , " h " ));
            l.Add(
new Obj( " 4 " , " y " ));
            l.Add(
new Obj( " 5 " , " j " ));
            l.Add(
new Obj( " 6 " , " r " ));
            l.Add(
new Obj( " 7 " , " c " ));
            l.Add(
new Obj( " 8 " , " d " ));

            
// 更简短的实现方法
            l.Sort(
                
delegate (Obj x, Obj y)
                {
                    
if (x == null )
                    {
                        
if (y == null )
                        {
                            
return 0 ;
                        }
                        
else
                        {
                            
return - 1 ;
                        }
                    }
                    
else if (y == null )
                    {
                        
return 1 ;
                    }
                    
else
                    {
                        
return x.B.CompareTo(y.B);
                    }
                });

            
// ----------------------------------------------------------------------------

            Console.WriteLine(
" \n- - - 排 序 后 2 - - - " );


            
foreach (Obj o in l)
            {
                Console.WriteLine(
" {0} - - - - - {1} " , o.A, o.B);
            }

            Console.WriteLine(
" \n- - - - - - - - - - 美 丽 的 分 隔 符 - - - - - - - - - - " );

            Console.ReadKey();

        }
    }

    
// 实现IComparer接口
     public class Comparer : IComparer < Obj >
    {
        
public int Compare(Obj x, Obj y)
        {
            
if (x == null )
            {
                
if (y == null )
                {
                    
return 0 ;
                }
                
else
                {
                    
return - 1 ;
                }
            }
            
else if (y == null )
            {
                
return 1 ;
            }
            
else
            {
                
return x.B.CompareTo(y.B);
            }
        }
    }

    
// 实体类
    [Serializable]
    
public class Obj
    {
        
private string a;
        
private string b;

        
public Obj( string _a, string _b)
        {
            a
= _a;
            b
= _b;
        }

        
public string A
        {
            
get { return a; }
        }

        
public string B
        {
            
get { return b; }
        }

    }
}

你可能感兴趣的:(list排序)