订购 ObservableCollection无需创建新的

发布于 2024-09-27 20:35:53 字数 287 浏览 2 评论 0原文

我有以下代码来重新排序 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灯角 2024-10-04 20:35:54

我的答案的灵感来自 bkk

 public static class ObservableCollection
    {
        public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector, bool isAZ)
        {
            if (isAZ)
            {
                List<TSource> sortedList = source.OrderBy(keySelector).ToList();
                source.Clear();
                foreach (var sortedItem in sortedList)
                {
                    source.Add(sortedItem);
                }
            }
            else
            {
                List<TSource> sortedList = source.OrderByDescending(keySelector).ToList();
                source.Clear();
                foreach (var sortedItem in sortedList)
                {
                    source.Add(sortedItem);
                }
            }         
        }
    }

用法

 _someObservableCollection.Sort(x => x.Number, false); // Where number is an simple property (non-object)

My answer is inspired by bkk

 public static class ObservableCollection
    {
        public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector, bool isAZ)
        {
            if (isAZ)
            {
                List<TSource> sortedList = source.OrderBy(keySelector).ToList();
                source.Clear();
                foreach (var sortedItem in sortedList)
                {
                    source.Add(sortedItem);
                }
            }
            else
            {
                List<TSource> sortedList = source.OrderByDescending(keySelector).ToList();
                source.Clear();
                foreach (var sortedItem in sortedList)
                {
                    source.Add(sortedItem);
                }
            }         
        }
    }

Usage

 _someObservableCollection.Sort(x => x.Number, false); // Where number is an simple property (non-object)
弃爱 2024-10-04 20:35:54

据我所知,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

审判长 2024-10-04 20:35:53

为 Observable 集合实现自定义排序扩展方法

 public static class ObservableCollection
 {
      public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector)
      {
          List<TSource> sortedList = source.OrderBy(keySelector).ToList();
          source.Clear();
          foreach (var sortedItem in sortedList)
          {
              source.Add(sortedItem);
          }
     }
 }

上面的答案受到 Jaider 先生对此的回复的启发 问题

Implement your custom sort extension method for Observable collection

 public static class ObservableCollection
 {
      public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector)
      {
          List<TSource> sortedList = source.OrderBy(keySelector).ToList();
          source.Clear();
          foreach (var sortedItem in sortedList)
          {
              source.Add(sortedItem);
          }
     }
 }

Above answer is inspired by Mr. Jaider's reply to this question

我的鱼塘能养鲲 2024-10-04 20:35:53

鉴于 OrderBy 还更新了一个数组以匹配集合的大小以及其他几个对象,您有两个选择:

  1. 完全放弃 LINQ OrderBy 并编写自己的就地执行排序使用 Move 方法对 ObservableCollection 进行排序。
  2. 等到当前的实现出现问题后再应用 1。

别担心,更新东西并没有那么可怕。 Linq 一直都是这么做的。除非遇到瓶颈,否则一切都好。除非有令人信服的证据表明就地排序确实会加快显示速度,否则这里没有问题。

Given that OrderBy also news up an array to match the size of your collection, and several other objects, you've two choices:

  1. Give up on LINQ OrderBy altogether and write your own sort that performs in-place sorting over your ObservableCollection using the Move method.
  2. Wait until the current implementation becomes problematic then apply 1.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文