用于对集合进行排序的代码段基于C#中逗号分隔的字符串值 [英] Codesnippet for sorting collection<T> based on a comma seperated string value in C#

查看:92
本文介绍了用于对集合进行排序的代码段基于C#中逗号分隔的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我的代码中,我将集合用作不同控件(WPF/C#)的绑定源.每当应用程序加载时,都会从系统创建该集合.我对收藏没有控制权,其顺序(收藏中的物品)随机存在 该应用的每次启动.

出于UI的原因,我需要允许对集合进行排序,将其显示在列表视图中,并在用户修改(上移和下移按钮)时保持排序.因此,我的想法很简单.我只是将逗号分隔成一个隐藏的字符串变量 例如. "itemX,ItemY,ItemZ".

现在,我需要一个可根据字符串对集合进行排序的功能.我当时在想几个foreach循环,但是我确信有一个更好的方式来对集合进行排序.

  • 按照字符串表示的顺序对集合中的项目进行排序.
T是接口,我可以访问属性名称".具有存储在字符串中的值,例如"ItemX".

有什么很棒的代码片段/功能吗?

谢谢

解决方案

为什么不列出另一个列表?并加载具有字符串属性的自定义对象,使用Linq对对象的属性进行排序并将结果绑定到控件?

http://msdn.microsoft.com/en-us/library/bb384054.aspx


Hi Everyone,

in my code I use a collection as binding source for different controls (WPF/C#). The collection is created from the system everytime the application loads. I don't have control on the collection and its order (items within the collection) are randomly for every launch of the app.

For UI Reasons I need to allow to sort the collection, display it in a listview and keep the sorting when modified by the user (moveup and movedown buttons). Therefore my Idea was simple. I simply write the items comma seperated into a hidden string variable eg. "itemX,ItemY,ItemZ".

Now I need a funktion that sorts the collection based on the string. I was thinking of a few foreach loops, but I am sure there is a better way of sorting the collection.

  • sort the Items within collection in the same order as represented by the string.

T is an Interface an I can Access a Property "Name" that has the value that is stored in the string eg. "ItemX".

Any cool snippets/functions?

Thanks

解决方案

Why don't you make another list<t> and load custom objects with string properties, use Linq to sort on an object's property and bind the results to the control?

http://msdn.microsoft.com/en-us/library/bb384054.aspx


这篇关于用于对集合进行排序的代码段基于C#中逗号分隔的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆