当前位置:   article > 正文

【c#编程技术总结】c#List排序补充_c# list倒序排列

c# list倒序排列

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群


 //方法一sort排序使用lambda表达式

  1. List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  2. list.Sort((x, y) => -x.CompareTo(y));//降序
  3. list.Sort((x, y) => x.CompareTo(y));//升序

  //方法二简单sort排序

  1. List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  2. list.Reverse();// 反转顺序
  3. list.Sort();// 升序排序

 //方法三复杂对象

  1. List<Student> list = new List<Student>();
  2. list.Sort(
  3. delegate (Student p1, Student p2)
  4. {
  5. return p1.sno.CompareTo(p2.sno);//升序
  6. //return p1.sno == p1.sno ? 0 : (p1.sno > p1.sno) ? 1 : -1;
  7. });
  8. //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });

 方法四OrdeOrderBy运用

  1. Debug.Log("****顺序排列****");
  2. var tlist = list.OrderBy(t => t.sno).ToList();
  3. Debug.Log("****倒序排列****");
  4. var tlist = list.OrderByDescending(t => t.sno).ToList();

方法五 chon重写Comparable

  1. public class Student: IComparable<Student>
  2. {
  3. public int sno;
  4. public string name;
  5. public Student(int sno, string name)
  6. {
  7. this.sno = sno;
  8. this.name = name;
  9. }
  10. //重写的CompareTo方法,根据Id排序
  11. public int CompareTo(Student other)
  12. {
  13. if (null == other)
  14. {
  15. return 1;//空值比较大,返回1
  16. }
  17. //return this.Id.CompareTo(other.Id);//升序
  18. return other.sno.CompareTo(this.sno);//降序
  19. }
  20. }

或者

  1. public int Compare(Student x, Student y)
  2. {
  3. return x.sno.CompareTo(y.sno);//升序
  4. }

测试脚本如下

  1. #region 模块信息
  2. // **********************************************************************
  3. // Copyright (C) 2019 Blazors
  4. // Please contact me if you have any questions
  5. // File Name: Test
  6. // Author:
  7. // WeChat||QQ:
  8. // **********************************************************************
  9. #endregion
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using UnityEngine;
  15. public class Student: IComparable<Student>
  16. {
  17. public int sno;
  18. public string name;
  19. public Student(int sno, string name)
  20. {
  21. this.sno = sno;
  22. this.name = name;
  23. }
  24. //重写的CompareTo方法,根据Id排序
  25. public int CompareTo(Student other)
  26. {
  27. if (null == other)
  28. {
  29. return 1;//空值比较大,返回1
  30. }
  31. //return this.Id.CompareTo(other.Id);//升序
  32. return other.sno.CompareTo(this.sno);//降序
  33. }
  34. public int Compare(Student x, Student y)
  35. {
  36. return x.sno.CompareTo(y.sno);//升序
  37. }
  38. }
  39. public class Test : MonoBehaviour
  40. {
  41. List<Student> targetList;
  42. // Use this for initialization
  43. void Start()
  44. {
  45. }
  46. private void Update()
  47. {
  48. //方法一
  49. if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表达式
  50. {
  51. List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  52. list.Sort((x, y) => -x.CompareTo(y));//降序
  53. list.Sort((x, y) => x.CompareTo(y));//升序
  54. }
  55. //方法二
  56. if (Input.GetKeyDown(KeyCode.W))//简单sort排序
  57. {
  58. List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  59. list.Reverse();// 反转顺序
  60. list.Sort();// 升序排序
  61. }
  62. //方法三
  63. if (Input.GetKeyDown(KeyCode.W))//简单sort排序
  64. {
  65. List<Student> list = new List<Student>();
  66. list.Sort(
  67. delegate (Student p1, Student p2)
  68. {
  69. return p1.sno.CompareTo(p2.sno);
  70. });
  71. //list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });
  72. }
  73. //方法三
  74. if (Input.GetKeyDown(KeyCode.Q))//OrderBy的运用
  75. {
  76. targetList = new List<Student>();
  77. for (int i = 0; i < 10; i++)
  78. {
  79. targetList.Add(new Student(i, "小明" + i));
  80. }
  81. var tList01 = OutOfOrder(targetList);
  82. var tList02 = InOrder(tList01);
  83. var tList03 = OutOfOrder(tList02);
  84. InvertedOrder(tList03);
  85. }
  86. }
  87. private List<Student> InOrder(List<Student> list)
  88. {
  89. Debug.Log("****顺序排列****");
  90. var tlist = list.OrderBy(t => t.sno).ToList();
  91. string str = ""; ;
  92. foreach (var item in tlist)
  93. {
  94. str += item.sno;
  95. }
  96. Debug.Log("顺序后学号:" + str);
  97. return tlist;
  98. }
  99. private List<Student> InvertedOrder(List<Student> list)
  100. {
  101. Debug.Log("****倒序排列****");
  102. var tlist = list.OrderByDescending(t => t.sno).ToList();
  103. string str = ""; ;
  104. foreach (var item in tlist)
  105. {
  106. str += item.sno;
  107. }
  108. Debug.Log("倒序后学号:" + str);
  109. return tlist;
  110. }
  111. /// <summary>
  112. /// List乱序
  113. /// </summary>
  114. /// <param name="a"></param>
  115. /// <returns></returns>
  116. public List<Student> OutOfOrder(List<Student> a)
  117. {
  118. Debug.LogError("****打乱列表****");
  119. List<Student> b = new List<Student>();
  120. int countNum = a.Count;
  121. //使用while循环,保证将a中的全部元素转移到b中而不产生遗漏
  122. while (b.Count < countNum)
  123. {
  124. //随机将a中序号为index的元素作为b中的第一个元素放入b中
  125. int index = UnityEngine.Random.Range(0, a.Count - 1);
  126. //检测是否重复,保险起见
  127. if (!b.Contains(a[index]))
  128. {
  129. //若b中还没有此元素,添加到b中
  130. b.Add(a[index]);
  131. //成功添加后,将此元素从a中移除,避免重复取值
  132. a.Remove(a[index]);
  133. }
  134. }
  135. string str = ""; ;
  136. foreach (var item in b)
  137. {
  138. str += item.sno;
  139. }
  140. Debug.Log("乱序后学号:" + str);
  141. return b;
  142. }
  143. }

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/95773
推荐阅读