开发者

C# - Sorting a ListBox containing key/value pairs using LINQ

I have a list box control that contains key value pairs delimited by an "=" sign.

Example:

hot=cold

fast=slow

high=low

blue=red

I have two buttons, one that will so开发者_StackOverflowrt the list by key and the other will sort the list by pair.

How could I do this using LINQ?


Sort by Key:

    myList.OrderBy(i => i.Split('=')[0])

Sort by Value:

    myList.OrderBy(i => i.Split('=')[1])


var sortedByKey = items.OrderBy(x => x.Split('=')[0]);
var sortedByValue = items.OrderBy(x => x.Split('=')[1]);

Those are queries that will order the items by the correct portion of the string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜