开发者

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

开发者 https://www.devze.com 2023-04-01 16:44 出处:网络
I have a list box control that contains key value pairs delimited by an \"=\" sign. Example: hot=cold fast=slow

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

精彩评论

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