.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));


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM