本篇內容介紹了“c#的List排序方法有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
//方法一sort排序使用lambda表達式
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Sort((x, y) => -x.CompareTo(y));//降序
list.Sort((x, y) => x.CompareTo(y));//升序//方法二簡單sort排序
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Reverse();// 反轉順序
list.Sort();// 升序排序//方法三復雜對象
List<Student> list = new List<Student>();
list.Sort(
delegate (Student p1, Student p2)
{
return p1.sno.CompareTo(p2.sno);//升序
//return p1.sno == p1.sno ? 0 : (p1.sno > p1.sno) ? 1 : -1;
});
//list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });方法四OrdeOrderBy運用
Debug.Log("****順序排列****");
var tlist = list.OrderBy(t => t.sno).ToList();
Debug.Log("****倒序排列****");
var tlist = list.OrderByDescending(t => t.sno).ToList();方法五 chon重寫Comparable
public class Student: IComparable<Student>
{
public int sno;
public string name;
public Student(int sno, string name)
{
this.sno = sno;
this.name = name;
}
//重寫的CompareTo方法,根據Id排序
public int CompareTo(Student other)
{
if (null == other)
{
return 1;//空值比較大,返回1
}
//return this.Id.CompareTo(other.Id);//升序
return other.sno.CompareTo(this.sno);//降序
}
}或者
public int Compare(Student x, Student y)
{
return x.sno.CompareTo(y.sno);//升序
}測試腳本如下
#region 模塊信息
// **********************************************************************
// Copyright (C) 2019 Blazors
// Please contact me if you have any questions
// File Name: Test
// Author:
// WeChat||QQ:
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class Student: IComparable<Student>
{
public int sno;
public string name;
public Student(int sno, string name)
{
this.sno = sno;
this.name = name;
}
//重寫的CompareTo方法,根據Id排序
public int CompareTo(Student other)
{
if (null == other)
{
return 1;//空值比較大,返回1
}
//return this.Id.CompareTo(other.Id);//升序
return other.sno.CompareTo(this.sno);//降序
}
public int Compare(Student x, Student y)
{
return x.sno.CompareTo(y.sno);//升序
}
}
public class Test : MonoBehaviour
{
List<Student> targetList;
// Use this for initialization
void Start()
{
}
private void Update()
{
//方法一
if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表達式
{
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Sort((x, y) => -x.CompareTo(y));//降序
list.Sort((x, y) => x.CompareTo(y));//升序
}
//方法二
if (Input.GetKeyDown(KeyCode.W))//簡單sort排序
{
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Reverse();// 反轉順序
list.Sort();// 升序排序
}
//方法三
if (Input.GetKeyDown(KeyCode.W))//簡單sort排序
{
List<Student> list = new List<Student>();
list.Sort(
delegate (Student p1, Student p2)
{
return p1.sno.CompareTo(p2.sno);
});
//list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });
}
//方法三
if (Input.GetKeyDown(KeyCode.Q))//OrderBy的運用
{
targetList = new List<Student>();
for (int i = 0; i < 10; i++)
{
targetList.Add(new Student(i, "小明" + i));
}
var tList01 = OutOfOrder(targetList);
var tList02 = InOrder(tList01);
var tList03 = OutOfOrder(tList02);
InvertedOrder(tList03);
}
}
private List<Student> InOrder(List<Student> list)
{
Debug.Log("****順序排列****");
var tlist = list.OrderBy(t => t.sno).ToList();
string str = ""; ;
foreach (var item in tlist)
{
str += item.sno;
}
Debug.Log("順序后學號:" + str);
return tlist;
}
private List<Student> InvertedOrder(List<Student> list)
{
Debug.Log("****倒序排列****");
var tlist = list.OrderByDescending(t => t.sno).ToList();
string str = ""; ;
foreach (var item in tlist)
{
str += item.sno;
}
Debug.Log("倒序后學號:" + str);
return tlist;
}
/// <summary>
/// List亂序
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public List<Student> OutOfOrder(List<Student> a)
{
Debug.LogError("****打亂列表****");
List<Student> b = new List<Student>();
int countNum = a.Count;
//使用while循環,保證將a中的全部元素轉移到b中而不產生遺漏
while (b.Count < countNum)
{
//隨機將a中序號為index的元素作為b中的第一個元素放入b中
int index = UnityEngine.Random.Range(0, a.Count - 1);
//檢測是否重復,保險起見
if (!b.Contains(a[index]))
{
//若b中還沒有此元素,添加到b中
b.Add(a[index]);
//成功添加后,將此元素從a中移除,避免重復取值
a.Remove(a[index]);
}
}
string str = ""; ;
foreach (var item in b)
{
str += item.sno;
}
Debug.Log("亂序后學號:" + str);
return b;
}
}“c#的List排序方法有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。