有没有遇到过产品经理说表格输出排序要按照指定的人员列表去排序?
经过一番研究搜查
发现一个方法可以实现
话不多说
上例子
public class UserInfo { public string Name { get; set; } public string Info { get; set; } }
List<UserInfo> userInfos = new List<UserInfo>{ new UserInfo { Name = "张三", Info = "篮球"},new UserInfo { Name = "李四", Info = "足球"},
new UserInfo { Name = "王五", Info = "羽毛球"},new UserInfo { Name = "赵六", Info = "网球"} }; string[] nameOrder = {"李四","赵六","王五","张三"}; userInfos = userInfos.OrderBy(e => { var index = 0; index = Array.IndexOf(nameOrder, e.Name); if (index != -1) { return index; } else { return int.MaxValue; } } ).ToList(); userInfos.ForEach(p => { Console.WriteLine(string.Format("Name:{0},Info:{1}",p.Name,p.Info)); } );
标签:Info,index,Name,自定义,C#,List,userInfos,UserInfo,new From: https://www.cnblogs.com/bbbbzai/p/17293320.html