linq按子串排序数组中的字符串

时间:2013-12-17 12:40:38

标签: c# arrays json linq

抱歉,我刚开始学习LINQ,如果你能说使用Linq按 ID BonusCount

string[] res;   // {"ID,Name, BonusCount",etc}    
res = new string[] {"1, Mark, 250", "4, Ostin, 150","2, Rick K., 12","11,Robert,1"};

我会得到:     由id:

{"1, Mark, 250", "2, Rick K., 12", "4, Ostin, 150","11,Robert,1"};

by BonusCount:

{"11,Robert,1", "2, Rick K., 12", "4, Ostin, 150", "1, Mark, 250"};I

有可能吗?提前谢谢!

1 个答案:

答案 0 :(得分:10)

是的,这是可能的,非常容易

 var res = new string[] { "1, Mark, 250", "4, Ostin, 150", "2, Rick K., 12", "11,Robert,1" };


 var sortByBonus = res.OrderBy(i => int.Parse(i.Split(',').Last())).ToArray();
 var sortById = res.OrderBy(i => int.Parse(i.Split(',').First())).ToArray();