List排序问题

deknight 2017-04-11 08:28:42
假设:
public class test
{
int count;
int rank;
}

list<test>
如何根据count排序,并重新为rank赋值??
...全文
167 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2017-04-12
  • 打赏
  • 举报
回复

List<test> datas = ....; 
var res = datas.OrderBy(d=>d.count).ToList();
csdnFUCKINGSUCKS 2017-04-11
  • 打赏
  • 举报
回复

List<model> list = new List<model>() { new model() { count = 9, rank = 10 }, new model() { count = 3, rank = 50 }, new model() { count = 12, rank = 30 } };

//根据count正序
var resultASC = list.OrderBy(l => l.count).ToList();
//根据count倒序
var resultDESC = list.OrderByDescending(l => l.count).ToList();
//给正序结果的rank赋值
resultASC.ForEach(i => { i.rank = Enumerable.Range(1, 500).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); });
//给倒序结果的rank赋值
resultDESC.ForEach(i => { i.rank = Enumerable.Range(1, 500).OrderBy(t => Guid.NewGuid()).FirstOrDefault(); });

foreach (var item in resultASC)
{
Console.WriteLine(item.count + "==>" + item.rank);
}

Console.WriteLine("---------");

foreach (var item in resultDESC)
{
Console.WriteLine(item.count + "==>" + item.rank);
}


Poopaye 2017-04-11
  • 打赏
  • 举报
回复
先排序 list.Sort((a, b) => a.count.CompareTo(b.count)); 再赋值 for(……) item.Rank = i;

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧