public struct student
{
public int num;
public int d;
public int c;
public student(int m, int n, int k)
{
num = m;
d = n;
c = k;
}
}
lst1.Sort((x, y) =>
{int value1= (y.d + y.c).CompareTo(x.d + x.c); ;
int value2= y.d.CompareTo(x.d);
if (value1 != 0) return value1;
else if (value2 != 0) return value2;
else return x.num.CompareTo(y.num);
});
例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
public struct student
{
public int num;
public int wt;
public student(int m,int n)
{
num = m;
wt = n;
}
}
static void Main(string[] args)
{
Random r = new Random();
List<student> lst = new List<student>();
for (int i = 0; i < 99; i++) lst.Add(new student(r.Next(0,32), r.Next(0, 32)));
foreach (student s in lst) Console.WriteLine(s.num + " " + s.wt);
Console.WriteLine("Sorted:------------------------");
lst.Sort((x,y)=>{ if (y.num.CompareTo(x.num) != 0) return y.num.CompareTo(x.num); else return x.wt.CompareTo(y.wt); });
foreach (student s in lst) Console.WriteLine(s.num+" "+s.wt);
Console.ReadKey();
}
}
}