zoukankan      html  css  js  c++  java
  • 调整数据列表内的顺序

     调整数据列表内的顺序,现写成扩展方法工具类

        public static class CollectionHelper
        {
            //交换List项的顺序
            public static bool ExchangeOrder<T>(this IList<T> list, int sourceID, int newID)
            {
                if (sourceID >= list.Count || sourceID < 0 || newID >= list.Count || newID < 0 || sourceID == newID) return false;
                try
                {
                    var temp = list[sourceID];
                    list[sourceID] = list[newID];
                    list[newID] = temp;
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
    
            /// <summary>
            /// 向上移动
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list">The 数据列表 list</param>
            /// <param name="id">The 需要移动的id</param>
            /// <param name="num">The 移动的 位数. 默认为1, 几位就向上移动几位</param>
            /// <returns></returns>
            public static bool MoveUpper<T>(this IList<T> list, int id, int num = 1)
            {
                return list.MoveItem(id, id - num);
            }
    
            //向下移动
            public static bool MoveDown<T>(this IList<T> list, int id, int num = 1)
            {
                return list.MoveItem(id, id + num);
            }
    
            //移动到顶部
            public static bool MoveTopper<T>(this IList<T> list, int id)
            {
                return list.MoveItem(id, 0);
            }
    
            //移动到底部
            public static bool MoveBottom<T>(this IList<T> list, int id)
            {
                return list.MoveItem(id, list.Count -1);
            }
    
            /// <summary>
            /// 移动
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="list">数据列表</param>
            /// <param name="sourceID">原来的数据ID</param>
            /// <param name="newID">移动后数据ID</param>
            /// <returns></returns>
            public static bool MoveItem<T>(this IList<T> list, int sourceID, int newID)
            {
                if (sourceID >= list.Count || sourceID < 0 || newID >= list.Count || newID < 0 || sourceID == newID) return false;
                try
                {
                    var temp = list[sourceID];
                    list.RemoveAt(sourceID);
                    list.Insert(newID, temp);
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    

    调用的方法:

         class CollectionSample
        {
    
            public void Test()
            {
                List<Student> stuList = new List<Student>();
                Student stu = new Student(1, "zhangSan");
                stuList.Add(stu);
                stu = new Student(2, "LiSi");
                stuList.Add(stu);
                stu = new Student(3, "WangWu");
                stuList.Add(stu);
                stu = new Student(4, "ZhangLiu");
                stuList.Add(stu);
                string msg = string.Empty;
                foreach (Student item in stuList)
                {
                    msg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);
                }
                //Exchange(stuList);
                Move(stuList);
            }
    
            private static void Exchange(List<Student> stuList)
            {
                CollectionHelper.ExchangeOrder(stuList, 2, 1);
    
                string newMsg = string.Empty;
                foreach (Student item in stuList)
                {
                    newMsg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);
                }
            }
    
            private static void Move(List<Student> stuList)
            {
                CollectionHelper.MoveUpper(stuList, 2, 3);
    
                string newMsg = string.Empty;
                foreach (Student item in stuList)
                {
                    newMsg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);
                }
            }
        }
    

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    win10 访问远程文件夹 此共享需要过时的SMB1协议 你不能访问此共享文件夹
    Navicat 1142 SELECT command denied to user 'sx'@'xxx' for table 'user'
    MySQL 密码参数配置与修改 validate_password
    MySQL 命令行下更好的显示查询结果
    MySQL 数据库的存储结构
    MySQL实验 内连接优化order by+limit 以及添加索引再次改进
    MySQL实验 子查询优化双参数limit
    MySQL 索引结构 hash 有序数组
    MySQL 树形索引结构 B树 B+树
    hbase2.1.9 centos7 完全分布式 搭建随记
  • 原文地址:https://www.cnblogs.com/linlf03/p/2553360.html
Copyright © 2011-2022 走看看