请教一下List的移除和排序问题

z46988646 2013-10-17 09:03:27
问题是这样的,现在有一个people类,类中有id,name等关键字,在主程序中创建一个List<people>,然后程序中要根据people的id来删除list中的people对象,还要依据id进行排序。
请问应该如何实现?
...全文
264 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
敌敌畏耶 2013-10-18
  • 打赏
  • 举报
回复
引用 4 楼 q107770540 的回复:
这明显是LINQ干的活
如果是2.0的呢···
showjim 2013-10-18
  • 打赏
  • 举报
回复
如果自己不想写平衡树,可以SortedDictionary<int, people>将就着用
小K的大师兄 2013-10-18
  • 打赏
  • 举报
回复
引用 3 楼 iyomumx 的回复:
list.RemoveAll(p => p.id == 要删除的id); //删除
list.Sort((p1,p2) => p1.id - p2.id);  //排序
要是在2.0下,就自己写一个排序的类。
 /// <summary>
    /// 继承IComparer<T>接口,实现同一自定义类型 对象比较
    /// </summary>
    /// <typeparam name="T">泛用类型</typeparam>
    public class Reverser<T> : IComparer<T>
    {
        private Type type = null;
        private string name=string.Empty;
        private Direction direction;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="type">进行比较的类类型</param>
        /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="direction">比较方向(升序/降序)</param>
        public Reverser(Type type, string name, Direction direction)
        {
            this.type = type;
            this.name = name;
            if (direction != Direction.ASC)
                this.direction = direction;
        }

        //必须实现IComparer<T>的比较方法。
        int IComparer<T>.Compare(T t1, T t2)
        {
            object x = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);
            object y = this.type.InvokeMember(this.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);
            if (this.direction != Direction.ASC)
                Swap(ref x, ref y);
            return (new CaseInsensitiveComparer()).Compare(x, y);
        }

        //交换操作数
        private void Swap(ref object x, ref object y)
        {
            object temp = null;
            temp = x;
            x = y;
            y = temp;
        }
    }

    /// <summary>
    /// 比较的方向,如下:
    /// ASC:升序
    /// DESC:降序
    /// </summary>
    public enum Direction
    {
        /// <summary>
        /// 升序
        /// </summary>
        ASC = 0,

        /// <summary>
        /// 降序
        /// </summary>
        DESC,
    }
用法:
private List<LaneSatusInfo> outLaneStatusIcon=new List<LaneSatusInfo>();
Reverser<LaneSatusInfo> reverserIcon = new Reverser<LaneSatusInfo>(typeof(LaneSatusInfo), "LaneID", Direction.ASC);
            //按车道号对出口的车道图标数据进行升序排列
            if (outLaneStatusIcon.Count != 0)
            {
                outLaneStatusIcon.Sort(reverserIcon);
            }

q107770540 2013-10-17
  • 打赏
  • 举报
回复
这明显是LINQ干的活
iyomumx 2013-10-17
  • 打赏
  • 举报
回复
list.RemoveAll(p => p.id == 要删除的id); //删除
list.Sort((p1,p2) => p1.id - p2.id);  //排序
wind_cloud2011 2013-10-17
  • 打赏
  • 举报
回复
排序:
  class people : IComparable
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int CompareTo(object obj)
            {
                int result;
                try
                {
                    people info = obj as people;
                    if (this.Id > info.Id)
                    {
                        result = 0;
                    }
                    else
                        result = 1;
                    return result;
                }
                catch (Exception ex) { throw new Exception(ex.Message); }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            List<people> PList = new List<people>();
            PList.Add(
                new people() { Id = 1, Name = "jack" });
            PList.Add(new people() { Id = 3, Name = "tom" });
            PList.Add(new people() { Id = 2, Name = "soya" });
            PList.Sort();
            foreach (var item in PList)
            {              
                listBox1.Items.Add(item.Id + ":" + item.Name);
            }
        }
threenewbee 2013-10-17
  • 打赏
  • 举报
回复
list = list.Where(x => x.id != xxx).OrderBy(x => x.id).ToList();

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧