user3111110
user3111110

Reputation:

linq sorted string in array by sub string

sorry I just started learning LINQ, and I would be glad if you would say it is possible to do using Linq sort this array by ID or BonusCount

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

I would get: by 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

Is it possible? Thanks in advance!

Upvotes: 4

Views: 903

Answers (1)

Ilya Sulimanov
Ilya Sulimanov

Reputation: 7836

Yes it is possible and quite easy

 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();

Upvotes: 10

Related Questions