C# List Sort 排序
2017-04-18 11:27
477 查看
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();
}
}
}
{
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();
}
}
}
相关文章推荐
- C#中List的排序用法(Sort)
- C# List.Sort()自定义比较方法排序
- C#List Sort多字段排序
- C#基础教程之IComparable用法,实现List<T>.sort()排序
- C# List.sort排序详解(多权重,升序降序)
- C#中List的排序用法(Sort)
- C#通过IComparable实现ListT.sort()排序
- C# list sort 排序
- C# 利用list.sort()进行排序
- C# 中 List.Sort运用(IComparer<T>)排序用法
- C# List<>.sort 排序【转载】
- C#中List<T>的排序用法(Sort)
- C#中List的排序(Sort)
- C#基础教程之IComparable用法,实现List<T>.sort()排序
- C# List<T>.Sort排序
- C# 排序 List.Sort
- C#中List的排序(Sort)
- C# 中 List.Sort运用(IComparer<T>)排序用法
- C# List.sort排序详解
- C#基础---IComparable用法,实现List<T>.sort()排序