C# List<>集合排序, 从大到小排序, 从小到大排序
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace ConsoleApp14
{
class Program
{
static void Main(string[] args)
{
List<Content> list1 = new List<Content>();
list1.Add(new Content { Id = 1, Title = "内容1" });
list1.Add(new Content { Id = 3, Title = "内容3" });
list1.Add(new Content { Id = 2, Title = "内容2" });
list1.Sort(SortCompare);
}
/// <summary>
/// 自定义排序
/// </summary>
/// <param name="info1"></param>
/// <param name="info2"></param>
/// <returns></returns>
private static int SortCompare(Content info1, Content info2)
{
//// Id 从小到大
//return info1.Id.CompareTo(info2.Id);
// Id 从大到小
return info2.Id.CompareTo(info1.Id);
}
}
public class Content
{
public int Id { get; set; }
public string Title { get; set; }
}
}