C# List 随机排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# List 随机排序相关的知识,希望对你有一定的参考价值。

 

1、List随机排序方法

  public List<T> RandomSortList<T>(List<T> ListT)
  {
    Random random = new Random();
    List<T> newList = new List<T>();
    foreach (T item in ListT)
    {
      newList.Insert(random.Next(newList.Count + 1), item);
    }
    return newList;
  }

2、随机生成题目

  /// <summary>
  /// 随机生成题目
  /// </summary>
  /// <returns></returns>
  public List<Entities.V_Quelibrary> GetRandomQue()
  {
    List<string> names = new List<string>();
    List<string> values = new List<string>();
    string itemId =Config.QueType;
    string hql = "from V_Quelibrary where state=1 and Item_Id=:Item_Id";
    names.Add("Item_Id");
    values.Add(itemId);
    List<Entities.V_Quelibrary> quelibraryList = queService.GetListQuelibraryNoPage(hql, names, values);
    List<Entities.V_Quelibrary> queList = new List<Entities.V_Quelibrary>();

    //筛选分值为2的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 2).ToList()).Take(2).ToList());

    //筛选分值为5的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 5).ToList()).Take(2).ToList());

    //筛选分值为8的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 8).ToList()).Take(2).ToList());

    //筛选分值为12的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 12).ToList()).Take(2).ToList());

    //筛选分值为20的随机两道题
    queList.AddRange(this.RandomSortList<Entities.V_Quelibrary>(quelibraryList.Where(t => t.Score == 20).ToList()).Take(2).ToList());

    //获得的list再次随机排序
    queList = this.RandomSortList<Entities.V_Quelibrary>(queList);
    return queList;
  }

 

以上是关于C# List 随机排序的主要内容,如果未能解决你的问题,请参考以下文章

C# 集合及集合内排序 问题 速求

C# DataGridView List集合 刷新

排序算法原理及代码实现(c#)

《C#零基础入门之百识百例》(八十四)系统类List列表类解析 -- 扑克排序

《C#零基础入门之百识百例》(八十四)系统类List列表类解析 -- 扑克排序

C# 与 F# 中的默认排序