调整数据列表内的顺序

简介:

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

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/archive/2012/06/18/2553360.html,如需转载请自行联系原作者


目录
打赏
0
0
0
0
51
分享
相关文章
重复值的判断标准是否可以根据具体业务需求进行调整?
重复值的判断标准需要紧密结合具体的业务需求进行灵活调整,这样才能确保数据处理的准确性和有效性,为业务决策提供可靠的数据支持。
140 58
一日一技:包含非hashable元素的列表如何去重并保持顺序?
一日一技:包含非hashable元素的列表如何去重并保持顺序?
141 0
Python 在不改变顺序的前提下,去除列表中相邻且重复的元素
Python 在不改变顺序的前提下,去除列表中相邻且重复的元素
算法练习第十题——寻找重复数(不修改数组)
给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。
SwiftUI—如何调整记录在List列表里的顺序
SwiftUI—如何调整记录在List列表里的顺序
289 0
SwiftUI—如何调整记录在List列表里的顺序
集合或映射迭代过程进行删除或修改操作的时候会导致并发异常
集合或映射迭代过程进行删除或修改操作的时候会导致并发异常
163 0
集合或映射迭代过程进行删除或修改操作的时候会导致并发异常
Excel 技术篇-跨页签统计某一区域下符合条件值的数量方法,COUNTIF函数、数量统计公式的用法实例演示
Excel 技术篇-跨页签统计某一区域下符合条件值的数量方法,COUNTIF函数、数量统计公式的用法实例演示
426 0
Excel 技术篇-跨页签统计某一区域下符合条件值的数量方法,COUNTIF函数、数量统计公式的用法实例演示
使用@AutoConfigureBefore调整配置顺序竟没生效?(中)
使用@AutoConfigureBefore调整配置顺序竟没生效?(中)