.NET C# 保持List有序:自带排序功能的List

1.工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure
{
    /// <summary>
    /// 自带排序功能的List
    /// T为简单类型时,系统已代为实现ICompareable<T>接口,
    /// T为自定义复杂类型时,需要手动实现ICompareable<T>接口
    /// </summary>
    /// <typeparam name="T">泛型类型</typeparam>
    public class SortedList<T>:List<T>
    {
        /// <summary>
        /// 自带排序功能的添加方法
        /// </summary>
        /// <param name="item">数据项</param>
        public new void Add(T item)//new 显式隐藏从基类继承过来的方法,进行重写方法时用到
        {
            int position = this.BinarySearch(item);
            if (position < 0)
            {
                position = ~position;//按位取反,获取项应插入集合的位置
            }

            this.Insert(position,item);
        }

        /// <summary>
        /// 自带排序功能的修改方法
        /// </summary>
        /// <param name="item">新数据项</param>
        /// <param name="index">被修改数据项的位置下标</param>
        public void ModifySorted(T item, int index)
        {
            this.RemoveAt(index);

            int position = this.BinarySearch(item);
            if (position < 0)
            {
                position = ~position; //按位取反,获取项应插入集合的位置
            }

            this.Insert(position,item);
        }

        //List<T>的删除方法不会干扰剩余数据项的排序顺序,所以不需要重写
    }
}
2.功能测试

//简单类型测试
            SortedList<int> intSortedList = new SortedList<int>();

//添加元素
            intSortedList.Add(200);
            intSortedList.Add(20);

            intSortedList.ForEach(Console.WriteLine);

//修改元素

intSortedList.ModifySorted(0, 1);
            intSortedList.ModifySorted(1, 0);

intSortedList.ForEach(Console.WriteLine);

//复杂类型测试

public class Student: IComparable<Student>//自定义复杂类型,如果想要使用List<T>的比较器,必须要继承IComparable<T>接口
    {
        /// <summary>
        /// 班级编号
        /// </summary>
        public int ClassNo { get; set; }
        /// <summary>
        /// 学号
        /// </summary>
        public int StudentNo { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string StudentName { get; set; }
        /// <summary>
        /// 年龄
        /// </summary>
        public int Age { get; set; }

        /// <summary>
        /// 比较方法规则
        /// </summary>
        public int CompareTo(Student obj)
        {
            return this.StudentNo.CompareTo(obj.StudentNo);
        }
    }

//添加

SortedList<Student> stuSortedList = new SortedList<Student>();
            for (int i = 0; i < 10; i++)
            {
                var stu = new Student();
                stu.ClassNo = i;
                stu.StudentNo = i;
                stu.StudentName = "三毛" + i;
                stu.Age = i;
                
                stuSortedList.Add(stu);
            }

            stuSortedList.ForEach(x => Console.WriteLine("ClassNo={0},StudentNo={1},StudentName={2},Age={3}",x.ClassNo,x.StudentNo,x.StudentName,x.Age));

//修改

for (int i = 0; i < 5; i++)
            {
                var stu = new Student();
                stu.ClassNo = 20 + i;
                stu.StudentNo = 20 + i;
                stu.StudentName = "胡三" + (20+ i);
                stu.Age = 20 + i;

                stuSortedList.ModifySorted(stu,i+1);
            }

            stuSortedList.ForEach(x => Console.WriteLine("ClassNo={0},StudentNo={1},StudentName={2},Age={3}", x.ClassNo,x.StudentNo, x.StudentName, x.Age));

上一篇:MySQL入门看这一篇就够了


下一篇:MySQL数据库知识汇总