添加Lesson16
This commit is contained in:
@@ -65,6 +65,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_lambda表达式",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_lambda表达式练习", "Lesson15_lambad表达式练习\Lesson15_lambda表达式练习.csproj", "{E0931580-D76C-4434-B059-149AEED828ED}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_lambda表达式练习", "Lesson15_lambad表达式练习\Lesson15_lambda表达式练习.csproj", "{E0931580-D76C-4434-B059-149AEED828ED}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_List排序", "Lesson16_List排序\Lesson16_List排序.csproj", "{8417820B-342A-423C-9865-29784EB5F4A6}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson16_List排序练习", "Lesson16_List排序练习\Lesson16_List排序练习.csproj", "{D6D1193A-668E-454A-BFBF-6186A2C76509}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -195,6 +199,14 @@ Global
|
|||||||
{E0931580-D76C-4434-B059-149AEED828ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{E0931580-D76C-4434-B059-149AEED828ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{E0931580-D76C-4434-B059-149AEED828ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{E0931580-D76C-4434-B059-149AEED828ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{E0931580-D76C-4434-B059-149AEED828ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E0931580-D76C-4434-B059-149AEED828ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{8417820B-342A-423C-9865-29784EB5F4A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{8417820B-342A-423C-9865-29784EB5F4A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{8417820B-342A-423C-9865-29784EB5F4A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{8417820B-342A-423C-9865-29784EB5F4A6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{D6D1193A-668E-454A-BFBF-6186A2C76509}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D6D1193A-668E-454A-BFBF-6186A2C76509}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D6D1193A-668E-454A-BFBF-6186A2C76509}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{D6D1193A-668E-454A-BFBF-6186A2C76509}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace ConsoleApp1
|
||||||
|
{
|
||||||
|
class shopItem
|
||||||
|
{
|
||||||
|
public int price;
|
||||||
|
|
||||||
|
public shopItem(int price)
|
||||||
|
{
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
List<shopItem> shopItems = new List<shopItem>();//创建一个List来存放商店物品
|
||||||
|
shopItems.Add(new shopItem(3));//往List中添加每个物品的价格
|
||||||
|
shopItems.Add(new shopItem(1));
|
||||||
|
shopItems.Add(new shopItem(2));
|
||||||
|
|
||||||
|
shopItems.Sort((a, b) => { return a.price > b.price ? 1 : -1; });//使用Sort方法对List进行排序,按照价格从低到高排序
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
namespace Lesson16_List排序
|
||||||
|
{
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
#region 知识点一 List自带的排序方法
|
||||||
|
List<int> list = new List<int>();
|
||||||
|
list.Add(3);
|
||||||
|
list.Add(2);
|
||||||
|
list.Add(4);
|
||||||
|
list.Add(6);
|
||||||
|
list.Add(5);
|
||||||
|
list.Add(1);
|
||||||
|
for (int i = 0; i < list.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(list[i]);
|
||||||
|
}
|
||||||
|
Console.WriteLine("_________________________");
|
||||||
|
//list提供了sort方法,可以对list进行排序,默认是升序排序
|
||||||
|
list.Sort();
|
||||||
|
for (int i = 0; i < list.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(list[i]);
|
||||||
|
}
|
||||||
|
//Arraylist中也有sort方法
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 知识点二 自定义类的排序
|
||||||
|
Console.WriteLine("_________________________");
|
||||||
|
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));
|
||||||
|
itemList.Sort();//无法排序,自定义类没有默认的排序规则,不知道按哪个字段排序
|
||||||
|
for (int i = 0; i < itemList.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(itemList[i].money);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 知识点三 通过委托函数进行排序
|
||||||
|
List<ShopItem> shopItems = new List<ShopItem>();
|
||||||
|
shopItems.Add(new ShopItem(5));
|
||||||
|
shopItems.Add(new ShopItem(2));
|
||||||
|
shopItems.Add(new ShopItem(3));
|
||||||
|
shopItems.Add(new ShopItem(6));
|
||||||
|
shopItems.Add(new ShopItem(4));
|
||||||
|
shopItems.Add(new ShopItem(1));
|
||||||
|
|
||||||
|
//通过委托函数进行排序
|
||||||
|
//shopItems.Sort(SortShopItem);
|
||||||
|
|
||||||
|
//通过匿名函数表达式进行排序
|
||||||
|
//shopItems.Sort(delegate(ShopItem a,ShopItem b) {
|
||||||
|
// if (a.price > b.price)
|
||||||
|
// {
|
||||||
|
// return 1;
|
||||||
|
// }
|
||||||
|
// else if (a.price == b.price)
|
||||||
|
// {
|
||||||
|
// return 0;
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// return -1;
|
||||||
|
// }
|
||||||
|
//});
|
||||||
|
|
||||||
|
//通过lambda表达式结合三木运算符进行排序
|
||||||
|
shopItems.Sort((a, b) => {
|
||||||
|
return a.price > b.price ? 1 : -1;
|
||||||
|
});
|
||||||
|
|
||||||
|
Console.WriteLine("_________________________");
|
||||||
|
for (int i = 0; i < shopItems.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(shopItems[i].price);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
static int SortShopItem(ShopItem a,ShopItem b)
|
||||||
|
{
|
||||||
|
if (a.price > b.price)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}else if (a.price == b.price)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Item : IComparable<Item>
|
||||||
|
{
|
||||||
|
public int money;
|
||||||
|
public Item(int money)
|
||||||
|
{
|
||||||
|
this.money = money;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo(Item other)
|
||||||
|
{
|
||||||
|
//返回值的含义:
|
||||||
|
//小于0:
|
||||||
|
//相当于放在当前对象前面
|
||||||
|
//等于0:
|
||||||
|
//保持当前的位置不变
|
||||||
|
//大于0:
|
||||||
|
//相当于放在当前对象后面
|
||||||
|
|
||||||
|
|
||||||
|
if ( this.money > other.money)
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}else if (this.money == other.money)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class ShopItem
|
||||||
|
{
|
||||||
|
public int price;
|
||||||
|
public ShopItem(int price)
|
||||||
|
{
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//总结:
|
||||||
|
//系统自带的变量(int, float, double......) 一般都可以直接 Sort
|
||||||
|
// 自定义类 Sort 有两种方式
|
||||||
|
// 1. 继承接口 IComparable
|
||||||
|
// 2. 在 Sort 中传入委托函数
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
namespace Lesson16_List排序练习
|
||||||
|
{
|
||||||
|
class Monster
|
||||||
|
{
|
||||||
|
public int atk;
|
||||||
|
public int def;
|
||||||
|
public int hp;
|
||||||
|
public Monster(int atk, int def, int hp)
|
||||||
|
{
|
||||||
|
this.atk = atk;
|
||||||
|
this.def = def;
|
||||||
|
this.hp = hp;
|
||||||
|
}
|
||||||
|
|
||||||
|
override public string ToString()
|
||||||
|
{
|
||||||
|
return $"怪物:atk={atk},def={def},hp={hp}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enum E_Quality
|
||||||
|
{
|
||||||
|
白色 = 0,
|
||||||
|
绿色 = 1,
|
||||||
|
蓝色 = 2,
|
||||||
|
紫色 = 3,
|
||||||
|
橘色 = 4
|
||||||
|
}
|
||||||
|
enum E_ItemType
|
||||||
|
{
|
||||||
|
武器 = 0,
|
||||||
|
护甲 = 1,
|
||||||
|
饰品 = 2
|
||||||
|
}
|
||||||
|
class Item
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public E_Quality quality;
|
||||||
|
public E_ItemType itemType;
|
||||||
|
|
||||||
|
public Item(string name, E_Quality quality, E_ItemType itemType)
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
this.quality = quality;
|
||||||
|
this.itemType = itemType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"物品:品质={quality},类型={itemType},名字={name}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
List<Monster> monsters = new List<Monster>();
|
||||||
|
Random r = new Random();
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
monsters.Add(new Monster(r.Next(1, 10), r.Next(1, 10), r.Next(1, 10)));
|
||||||
|
Console.WriteLine(monsters[i]);
|
||||||
|
}
|
||||||
|
Console.WriteLine("_______________________________");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int inputIndex = int.Parse(Console.ReadLine());
|
||||||
|
switch (inputIndex)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
monsters.Sort((m1, m2) => m1.atk - m2.atk);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
monsters.Sort((m1, m2) => m1.def - m2.def);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
monsters.Sort((m1, m2) => m1.hp - m2.hp);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
//反转API
|
||||||
|
monsters.Reverse();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("ERROR");
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(monsters[i]);
|
||||||
|
}
|
||||||
|
Console.WriteLine("_______________________________");
|
||||||
|
|
||||||
|
List<Item> items = new List<Item>();
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
E_Quality randomQuality = (E_Quality)r.Next(0, Enum.GetValues(typeof(E_Quality)).Length);
|
||||||
|
E_ItemType randomItemType = (E_ItemType)r.Next(0, Enum.GetValues(typeof(E_ItemType)).Length);
|
||||||
|
|
||||||
|
items.Add(new Item("Item" + r.Next(100, 201), randomQuality, randomItemType));
|
||||||
|
Console.WriteLine(items[i]);
|
||||||
|
}
|
||||||
|
Console.WriteLine("_______________________________");
|
||||||
|
items.Sort((i1, i2) => {
|
||||||
|
//先按类型比
|
||||||
|
if (i1.itemType != i2.itemType)
|
||||||
|
{
|
||||||
|
return i1.itemType - i2.itemType;
|
||||||
|
}
|
||||||
|
//再按品质比
|
||||||
|
else if (i1.quality != i2.quality)
|
||||||
|
{
|
||||||
|
return i2.quality - i1.quality;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return i1.name.CompareTo(i2.name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (int i = 0; i < 100; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(items[i]);
|
||||||
|
}
|
||||||
|
Console.WriteLine("_______________________________");
|
||||||
|
|
||||||
|
Dictionary<int, string> dic = new Dictionary<int, string>();
|
||||||
|
dic.Add(1, "532132");
|
||||||
|
dic.Add(3, "532132");
|
||||||
|
dic.Add(4, "532132");
|
||||||
|
dic.Add(2, "532132");
|
||||||
|
dic.Add(6, "532132");
|
||||||
|
dic.Add(5, "532132");
|
||||||
|
|
||||||
|
foreach (var kv in dic)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Key={kv.Key},Value={kv.Value}");
|
||||||
|
}
|
||||||
|
Console.WriteLine("_______________________________");
|
||||||
|
List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();
|
||||||
|
|
||||||
|
foreach (KeyValuePair<int,string> item in dic)
|
||||||
|
{
|
||||||
|
list.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
list.Sort((p1, p2) =>
|
||||||
|
{
|
||||||
|
return p1.Key - p2.Key;
|
||||||
|
});
|
||||||
|
|
||||||
|
for(int i = 0;i < list.Count;i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(list[i].Key + " " + list[i].Value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user