List<int> list = new List<int>() { 3, 2, 6, 1, 4, 5 }; string info = "排序前:"; for (int i = 0; i < list.Count; i++) { info += list[i]; info += ","; } Console.WriteLine(info);
//list提供的排序方法(默认升序) list.Sort(); info = "排序后:"; for (int i = 0; i < list.Count; i++) { info += list[i]; info += ","; } Console.WriteLine(info);
输出:
1 2
排序前:3,2,6,1,4,5, 排序后:1,2,3,4,5,6,
自定义类的排序
对于下面这种自定义类,List<> 自带的 Sort() 方法是不能直接排序的,会报错
1 2 3 4 5 6 7 8
classItem { public int money; public Item(int money) { this.money = money; } }
Unhandled exception. System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable. at System.Collections.Comparer.Compare(Object a, Object b) at System.Collections.Generic.ArraySortHelper`1.InsertionSort(Span`1 keys, Comparison`1 comparer) at System.Collections.Generic.ArraySortHelper`1.IntroSort(Span`1 keys, Int32 depthLimit, Comparison`1 comparer) at System.Collections.Generic.ArraySortHelper`1.IntrospectiveSort(Span`1 keys, Comparison`1 comparer) at System.Collections.Generic.ArraySortHelper`1.Sort(Span`1 keys, IComparer`1 comparer) --- End of inner exception stack trace --- at System.Collections.Generic.ArraySortHelper`1.Sort(Span`1 keys, IComparer`1 comparer) at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer) at System.Collections.Generic.List`1.Sort(Int32 index, Int32 count, IComparer`1 comparer) at Program.<Main>$(String[] args) in e:\CodeField\CSharpProjects\CSharpTest\Program.cs:line 8
List<Item> itemList = new List<Item>(); itemList.Add(new Item(45)); itemList.Add(new Item(10)); itemList.Add(new Item(99)); itemList.Add(new Item(24)); itemList.Add(new Item(100)); itemList.Add(new Item(12)); string info = "排序前:"; for (int i = 0; i < itemList.Count; i++) { info += itemList[i].money; info += ","; } Console.WriteLine(info);
//list提供的排序方法(默认升序) itemList.Sort(); info = "排序后:"; for (int i = 0; i < itemList.Count; i++) { info += itemList[i].money; info += ","; } Console.WriteLine(info);
List<ShopItem> shopItems = new List<ShopItem>(); shopItems.Add(new ShopItem(2)); shopItems.Add(new ShopItem(1)); shopItems.Add(new ShopItem(4)); shopItems.Add(new ShopItem(3)); shopItems.Add(new ShopItem(6)); shopItems.Add(new ShopItem(5)); string info = "排序前:"; for (int i = 0; i < shopItems.Count; i++) { info += shopItems[i].id; info += ","; } Console.WriteLine(info);
//list提供的排序方法(默认升序) shopItems.Sort(SortShopItem); info = "排序后:"; for (int i = 0; i < shopItems.Count; i++) { info += shopItems[i].id; info += ","; } Console.WriteLine(info);
List<ShopItem> shopItems = new List<ShopItem>(); shopItems.Add(new ShopItem(2)); shopItems.Add(new ShopItem(1)); shopItems.Add(new ShopItem(4)); shopItems.Add(new ShopItem(3)); shopItems.Add(new ShopItem(6)); shopItems.Add(new ShopItem(5)); string info = "排序前:"; for (int i = 0; i < shopItems.Count; i++) { info += shopItems[i].id; info += ","; } Console.WriteLine(info);
//list提供的排序方法(默认升序) shopItems.Sort((a, b) => { return a.id > b.id ? 1 : -1; }); info = "排序后:"; for (int i = 0; i < shopItems.Count; i++) { info += shopItems[i].id; info += ","; } Console.WriteLine(info);