using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CShapeTest
{
class Hero
{
private int age;
private string name;
private int level;
private int power;
public Hero(int age, string name, int level, int power)
{
this.age = age;
this.name = name;
this.level = level;
this.power = power;
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Level
{
get
{
return level;
}
set
{
level = value;
}
}
public int Power
{
get
{
return power;
}
set
{
power = value;
}
}
public override string ToString()
{
return string.Format("age:{0}, name:{1}, level:{2}, power:{3}", this.age, this.name, this.level, this.power);
}
}
class Start
{
static void Main(string[] args)
{
List<Hero> personList = new List<Hero>();
personList.Add(new Hero(25, "cxm", 10, 30));
personList.Add(new Hero(24, "zqr", 12, 40));
personList.Add(new Hero(27, "xiaoming", 13, 50));
personList.Add(new Hero(12, "zhangsan", 52, 10));
personList.Add(new Hero(20, "lisi", 10, 20));
personList.Add(new Hero(15, "chenglong", 11, 5));
personList.Add(new Hero(12, "yunlong", 10, 59));
// 利用匿名方法对list排序(单权重升序)
//personList.Sort(delegate(Hero h1, Hero h2)
//{
// return h1.Age.CompareTo(h2.Age);
//});
// 利用Lambda方法对list排序(单权重升序)
personList.Sort((Hero h1, Hero h2) =>
{
return h1.Age.CompareTo(h2.Age);
});
foreach (var item in personList)
{
Console.WriteLine(item);
}
Console.WriteLine("Count:" + personList.Count);
Console.ReadLine();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace CShapeTest
{
class Hero
{
private int age;
private string name;
private int level;
private int power;
public Hero(int age, string name, int level, int power)
{
this.age = age;
this.name = name;
this.level = level;
this.power = power;
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Level
{
get
{
return level;
}
set
{
level = value;
}
}
public int Power
{
get
{
return power;
}
set
{
power = value;
}
}
public override string ToString()
{
return string.Format("age:{0}, name:{1}, level:{2}, power:{3}", this.age, this.name, this.level, this.power);
}
}
class Start
{
static void Main(string[] args)
{
List<Hero> personList = new List<Hero>();
personList.Add(new Hero(25, "cxm", 10, 30));
personList.Add(new Hero(24, "zqr", 12, 40));
personList.Add(new Hero(27, "xiaoming", 13, 50));
personList.Add(new Hero(12, "zhangsan", 52, 10));
personList.Add(new Hero(20, "lisi", 10, 20));
personList.Add(new Hero(15, "chenglong", 11, 5));
personList.Add(new Hero(12, "yunlong", 10, 59));
// 利用匿名方法对list排序(多权重升序)
//personList.Sort(delegate(Hero h1, Hero h2)
//{
// if (h1.Age < h2.Age)
// {
// return -1;//左值小于右值,返回-1,为升序,如果返回1,就是降序
// }
// else if (h1.Age == h2.Age)//左值等于右值,返回0
// {
// if (h1.Level < h2.Level)
// {
// return -1;
// }
// else if (h1.Level == h2.Level)
// {
// return 0;
// }
// else
// {
// return 1;
// }
// }
// else
// {
// return 1;//左值大于右值,返回1,为升序,如果返回-1,就是降序
// }
//});
// 利用Lambda方法对list排序(多权重升序)
personList.Sort((Hero h1, Hero h2) =>
{
if (h1.Age < h2.Age)
{
return -1;//左值小于右值,返回-1,为升序,如果返回1,就是降序
}
else if (h1.Age == h2.Age)//左值等于右值,返回0
{
if (h1.Level < h2.Level)
{
return -1;
}
else if (h1.Level == h2.Level)
{
return 0;
}
else
{
return 1;
}
}
else
{
return 1;//左值大于右值,返回1,为升序,如果返回-1,就是降序
}
});
foreach (var item in personList)
{
Console.WriteLine(item);
}
Console.WriteLine("Count:" + personList.Count);
Console.ReadLine();
}
}
}