委托 lambda表达式 类继承接口
class Program
{
public static void Main()
{
List tests = new List();
tests.Add(new Test (1));
tests.Add(new Test (4));
tests.Add(new Test (2));
tests.Add(new Test (3));
tests.Add(new Test (5));
//用委托
tests.Sort( delegate (Test a, Test b)
{
if (a.Id > b.Id)
{
return -1;
}
else if (a.Id < b.Id)
{ return 1; }
return 0;
});
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].Id);
}
Console.WriteLine("-----------------------------------------");
// 用lambda表达式
tests.Clear();
tests.Add(new Test(1));
tests.Add(new Test(4));
tests.Add(new Test(2));
tests.Add(new Test(3));
tests.Add(new Test(5));
tests .Sort( (a, b)=>
{
if(a.Id>b.Id)
return 1;
return -1;
});
//把if else 写成三目运算符
tests.Sort((a, b) => { return (a.Id > b.Id) ? 1 : -1; });
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].Id);
}
Console.WriteLine("-----------------------------------------");
//用类继承接口 实现好接口里的逻辑后就可以直接用啦
tests.Clear();
tests.Add(new Test(1));
tests.Add(new Test(4));
tests.Add(new Test(2));
tests.Add(new Test(3));
tests.Add(new Test(5));
tests.Sort();
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i].Id);
}
}
}
class Test : IComparable
{
public int Id { get; set; }
public Test( int id)
{
Id = id;
}
public int CompareTo(Test other)
{ // 返回正数(1) 放后面
//返回负数(-1) 放前面
if(this.Id>other.Id) //返回0 不动
return 1; //如果我比他大 我就放他后面(返回1)
else if(this.Id
根据不同词条排序
class Program
{
public static void Main()
{
Practice1 practice1 = new Practice1();
practice1.MonsterSort();
}
}
class Practice1
{
int positive = 1;
int negative = -1;
int a;
List list = new List();
public Practice1()
{
list.Add(new Monster(1, 3, 6));
list.Add(new Monster(2, 4, 8));
list.Add(new Monster(7, 7, 7));
list.Add(new Monster(5, 6, 3));
list.Add(new Monster(7, 9, 1));
list.Add(new Monster(4, 5, 1));
list.Add(new Monster(3, 3, 5));
list.Add(new Monster(11, 3, 9));
list.Add(new Monster(5, 11, 10));
list.Add(new Monster(6, 10, 12));
}
public void MonsterSort()
{
while (true)
{
Console.WriteLine("");
Console.WriteLine("请输入数字进行排序");
Console.WriteLine("1.攻击排序");
Console.WriteLine("2.防御排序");
Console.WriteLine("3.血量排序");
Console.WriteLine("4.反转");
a = int.Parse(Console.ReadLine());
if (a == 4)
{
positive = -positive;
negative = -negative;
}
else
{
list.Sort(SortFun);
}
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
}
}
public int SortFun(Monster c ,Monster b)
{
switch (a)
{
case 1:
return (c.Atk > b.Atk ? positive : negative);
case 2:
return (c.Def > b.Def ? positive : negative);
case 3:
return (c.Def > b.Def ? positive : negative);
}
return 0;
}
}
class Monster
{
public int Atk { get; set; }
public int Def { get; set; }
public int Hp { get; set; }
public Monster(int atk ,int def ,int hp)
{
Atk = atk;
Def = def;
Hp = hp;
}
public override string ToString()
{
return String.Format("怪物血量{0} 攻击力{1} 防御力{2}",Hp,Atk,Def);
}
}
根据不同词条的权重排序
(类型>名字长度>品质)
class Program
{
public static void Main()
{
Practice1 practice2 = new Practice2();
practice1.ObjectsSort();
}
}
namespace Advanced
{
internal class practice2
{
List practice=new List();
public practice2()
{
practice.Add(new Objects(Types.Weapon, "手枪", Value.C));
practice.Add(new Objects(Types.Weapon, "散弹枪", Value.B));
practice.Add(new Objects(Types.Weapon, "神威大炮", Value.A));
practice.Add(new Objects(Types.Armor, "皮衣", Value.C));
practice.Add(new Objects(Types.Jewelry, "碎骨项链", Value.F));
practice.Add(new Objects(Types.Armor, "铁胸甲", Value.B));
practice.Add(new Objects(Types.Weapon, "小步枪", Value.C));
practice.Add(new Objects(Types.Jewelry, "麻痹戒指", Value.A));
practice.Add(new Objects(Types.Armor, "铁鞋子", Value.B));
practice.Add(new Objects(Types.Jewelry, "雷电手环", Value.A));
practice.Add(new Objects(Types.Weapon, "大AK47", Value.A));
}
public void ObjectsSort()
{
practice.Sort((a, b) => {
if (a.objectType > b.objectType)
return -1;
else if(a.objectTypeb.name.Length)
return -1;
else if(a.name.Length < b.name.Length)
return 1;
else if(a.value
把字典键值对存List 里面根据键(或者值进行排序)
namespace Advanced
{
internal class practice3
{
Dictionary dir = new Dictionary();
List> pairsList = new List>();
public practice3()
{
dir.Add(4, "abc");
dir.Add(1, "bcd");
dir.Add(2, "cde");
dir.Add(3, "def");
}
public void DirectorySort()
{
foreach (KeyValuePair a in dir)
{
pairsList.Add(a);
}
pairsList.Sort((a, b) =>
{
if (a.Key < b.Key)
{
return -1;
}
else
return 1;
});
foreach (KeyValuePair a in pairsList)
{
Console.WriteLine("{0}-{1}",a.Key,a.Value);
}
}
}
}