目錄
前言:
值類型的排序:
方法一:直接調(diào)用sort函數(shù)
方法二:通過C# ling表達(dá)式與CompareTo接口配合使用
方法三:降序的實(shí)現(xiàn)
對于自定義類型的sort排序?
方法一:通過實(shí)現(xiàn)IComparable接口重寫CompareTo方法來排序
方法二:通過ling表達(dá)式實(shí)現(xiàn)?
前言:
????????有時需要對List列表中內(nèi)容進(jìn)行排序,List 的Sort方法排序有三種結(jié)果 1,0,-1分別表示大于,等于,小于。List的sort的方法如何使用,如何實(shí)現(xiàn)降序和升序
值類型的排序:
方法一:直接調(diào)用sort函數(shù)
?static void Main(string[] args)
? ?{? ? ? ? ? ? List<int> sortList = new List<int>() { 100, 101, 50, 4 };
? ? ? ? ? ? sortList.Sort();? ? ? ? ? ? foreach (var i in sortList)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? sortList.Sort((a, b) => a.CompareTo(b));
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }
輸出結(jié)果: 4,50,100,101,默認(rèn)情況下,通過sort排序是升序?
方法二:通過C# ling表達(dá)式與CompareTo接口配合使用
sortList.Sort((a, b) => a.CompareTo(b)); //升序排序,與sortList.Sort()結(jié)果一樣
方法三:降序的實(shí)現(xiàn)
可以直接在對比較結(jié)果取負(fù)值
?sortList.Sort((a, b) => -a.CompareTo(b)); //結(jié)果取負(fù)值,升序就變成了降序了
查看int32源碼我們看到,int32 是實(shí)現(xiàn)了CompareTo接口的
public struct Int32 : IComparable, IFormattable, IConvertible, IComparable<int>, IEquatable<int>
? {
? ? internal int m_value;
? ? [__DynamicallyInvokable]
? ? public const int MaxValue = 2147483647;
? ? [__DynamicallyInvokable]
? ? public const int MinValue = -2147483648;? ? public int CompareTo(object value)
? ? {
? ? ? if (value == null)
? ? ? ? return 1;
? ? ? if (!(value is int num))
? ? ? ? throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt32"));
? ? ? if (this < num)
? ? ? ? return -1;
? ? ? return this > num ? 1 : 0;
? ? }? ? [__DynamicallyInvokable]
? ? public int CompareTo(int value)
? ? {
? ? ? if (this < value)
? ? ? ? return -1;
? ? ? return this > value ? 1 : 0;
? ? }}
對于自定義類型的sort排序?
方法一:通過實(shí)現(xiàn)IComparable接口重寫CompareTo方法來排序
? ? public class Dog : IComparable<Dog>
? ? {
? ? ? ? private float _price;? ? ? ? public float price => _price;
? ? ? ? public Dog(float price)
? ? ? ? {
? ? ? ? ? ? _price = price;
? ? ? ? }? ? ? ? public int CompareTo(Dog other)
? ? ? ? {
? ? ? ? ? ? if (_price > other.price)
? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? if (_price == other.price)
? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? return -1;
? ? ? ? }? ? ? ? public override string ToString()
? ? ? ? {
? ? ? ? ? ? return _price.ToString();
? ? ? ? }
? ? }
?文章來源地址http://www.zghlxwxcb.cn/news/detail-615468.html
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? List<Dog> dogs = new List<Dog>();
? ? ? ? ? ? dogs.Add(new Dog(12));
? ? ? ? ? ? dogs.Add(new Dog(10));
? ? ? ? ? ? dogs.Add(new Dog(14));? ? ? ? ? ? dogs.Sort();
? ? ? ? ? ? foreach (var i in dogs)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }文章來源:http://www.zghlxwxcb.cn/news/detail-615468.html
輸出結(jié)果:為 10,12,14,
方法二:通過ling表達(dá)式實(shí)現(xiàn)?
? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? List<Dog> dogs = new List<Dog>();
? ? ? ? ? ? dogs.Add(new Dog(12));
? ? ? ? ? ? dogs.Add(new Dog(10));
? ? ? ? ? ? dogs.Add(new Dog(14));? ? ? ? ? ? dogs.Sort((a, b) =>
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (a.price > b.price)
? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? if (a.price == b.price)
? ? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? });? ? ? ? ? ? foreach (var i in dogs)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write(i);
? ? ? ? ? ? ? ? Console.Write(",");
? ? ? ? ? ? }
? ? ? ? ? ? Thread.Sleep(10000);
? ? ? ? }
?
到了這里,關(guān)于C# list的sort排序的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!