• 泛型排序 [转帖]


    泛型 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; }
            }

        }
  • 相关阅读:
    数组对象---数据存储
    运行vue项目时,无法自动打开页面怎么办?
    数组扁平化
    数组去重
    CSS-----------text-transform
    CSS3-----width:max-content,min-content和fit_content属性
    可迭代对象
    bit和byte的 区别?
    前端常见跨域问题?
    HackerRank "Lucky Numbers"
  • 原文地址:https://www.cnblogs.com/liangwei389/p/1343400.html
Copyright © 2020-2023  润新知