订购 ObservableCollection无需创建新的
我有以下代码来重新排序 ObservableCollection
集合:
list = new ObservableCollection<SomeType>( list.OrderBy( c=>c.Ordinal ) );
此代码有效,但我不喜欢涉及“新”这一事实。有没有一种方法可以更改 ObservableCollection
集合的内部元素顺序,而无需创建新集合?
谢谢,
I have following code to re-order a ObservableCollection<T>
collection:
list = new ObservableCollection<SomeType>( list.OrderBy( c=>c.Ordinal ) );
This code works, but I don't like the fact that "new" is involved. Is there a way I can change the internal element order of a ObservableCollection<T>
collection without creating a new one?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的答案的灵感来自 bkk
用法
My answer is inspired by bkk
Usage
据我所知,ObservableCollection 有在集合内移动项目的方法。您应该为您的 T 类型获取一个比较器,然后使用某种排序算法
As far as I can tell, ObservableCollection has method for moving items inside the collection. You should get a comparer for your T-type, and then use some sorting algorithm
为 Observable 集合实现自定义排序扩展方法
上面的答案受到 Jaider 先生对此的回复的启发 问题
Implement your custom sort extension method for Observable collection
Above answer is inspired by Mr. Jaider's reply to this question
鉴于
OrderBy
还更新了一个数组以匹配集合的大小以及其他几个对象,您有两个选择:别担心,更新东西并没有那么可怕。 Linq 一直都是这么做的。除非遇到瓶颈,否则一切都好。除非有令人信服的证据表明就地排序确实会加快显示速度,否则这里没有问题。
Given that
OrderBy
also news up an array to match the size of your collection, and several other objects, you've two choices:Don't worry, newing stuff up isn't so terrible. Linq does it all the time. Unless it's a bottleneck, all is good. Unless there's compelling evidence that sorting in-place will really speed up the show, then there's no problem here.