有Family的类如下:
public class FamilyModel { public string Name { set; get; } }
创建List
List<FamilyModel> listFamily = new List<FamilyModel>(); listFamily.Add(new FamilyModel { Name="bbb"}); listFamily.Add(new FamilyModel { Name = "abc" }); listFamily.Add(new FamilyModel { Name = "fgd" });
想要按Name把这个List<FamilyModel>排序:
// 直接更改当前List listFamily.Sort((x, y) => string.Compare(x.Name, y.Name)); // 生成新的List var newList = listFamily.OrderBy(x => x.Name).ToList(); // ToList optional
方法来自:http://stackoverflow.com/questions/188141/c-sharp-list-orderby-alphabetical-order
如果需要多个字段排序:
http://stackoverflow.com/questions/3047455/linq-orderby-with-more-than-one-field