.NET C# 保持List<T>有序:自带排序功能的List<T>
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了.NET C# 保持List<T>有序:自带排序功能的List<T>,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3524字,纯文字阅读大概需要6分钟。
内容图文
![.NET C# 保持List<T>有序:自带排序功能的List<T>](/upload/InfoBanner/zyjiaocheng/685/8b891d43429b4ef6a76b3732cb9c9498.jpg)
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));
内容总结
以上是互联网集市为您收集整理的.NET C# 保持List<T>有序:自带排序功能的List<T>全部内容,希望文章能够帮你解决.NET C# 保持List<T>有序:自带排序功能的List<T>所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。