zoukankan      html  css  js  c++  java
  • SortableObservableCollection<T>

    View Code
    1 public class SortableObservableCollection<T> : ObservableCollection<T>
    2 {
    3 public SortableObservableCollection(List<T> list)
    4 : base(list)
    5 {
    6 }
    7
    8 public SortableObservableCollection(IEnumerable<T> collection)
    9 : base(collection)
    10 {
    11 }
    12
    13 public void Sort<TKey>(Func<T, TKey> keySelector, System.ComponentModel.ListSortDirection direction)
    14 {
    15 switch (direction)
    16 {
    17 case System.ComponentModel.ListSortDirection.Ascending:
    18 {
    19 ApplySort(Items.OrderBy(keySelector));
    20 break;
    21 }
    22 case System.ComponentModel.ListSortDirection.Descending:
    23 {
    24 ApplySort(Items.OrderByDescending(keySelector));
    25 break;
    26 }
    27 }
    28 }
    29
    30 public void Sort<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer)
    31 {
    32 ApplySort(Items.OrderBy(keySelector, comparer));
    33 }
    34
    35 private void ApplySort(IEnumerable<T> sortedItems)
    36 {
    37 var sortedItemsList = sortedItems.ToList();
    38
    39 foreach (var item in sortedItemsList)
    40 {
    41 Move(IndexOf(item), sortedItemsList.IndexOf(item));
    42 }
    43 }
    44 }

    View Code
    1 /// <summary>
    2 /// DESCRIPTION
    3 /// </summary>
    4   public class SortableObservableCollection<T> : ObservableCollection<T>
    5 {
    6 private bool sorting;
    7
    8 public void Sort()
    9 {
    10 this.Sort(Comparer<T>.Default);
    11 }
    12
    13 public void Sort(IComparer<T> comparer)
    14 {
    15 this.sorting = true;
    16
    17 int i, j;
    18 for (i = 1; i < this.Count; i++)
    19 {
    20 T index = this[i];
    21 j = i;
    22 while ((j > 0) && (comparer.Compare(this[j - 1], index) == 1))
    23 {
    24 this[j] = this[j - 1];
    25 j = j - 1;
    26 }
    27 this[j] = index;
    28 }
    29
    30 this.sorting = false;
    31
    32 this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    33 }
    34
    35 protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    36 {
    37 if (this.sorting == false)
    38 {
    39 base.OnCollectionChanged(e);
    40 }
    41 }
    42 }
    View Code
    1 /// <summary>
    2 /// Create a instance of SortableObservableCollection.
    3 /// </summary>
    4 /// <typeparam name="T">Collection</typeparam>
    5   public class SortableObservableCollection<T> : ObservableCollection<T>
    6 {
    7 public virtual void Sort()
    8 {
    9 this.Sort(0, Count, null);
    10 }
    11
    12 public virtual void Sort(IComparer<T> comparer)
    13 {
    14 this.Sort(0, Count, comparer);
    15 }
    16
    17 public virtual void Sort(int index, int count, IComparer<T> comparer)
    18 {
    19 (this.Items as List<T>).Sort(index, count, comparer);
    20 OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    21 }
    22 }

  • 相关阅读:
    Android获取视频音频的时长的方法
    Android动画效果之Frame Animation(逐帧动画)
    去除自定义Toolbar中左边距
    Android Toolbar样式定制详解
    Android 5.x Theme 与 ToolBar 实战
    Android ToolBar 使用完全解析
    Android开发:最详细的 Toolbar 开发实践总结
    SpannableString 转换局部字体大小,但在EditText测量之前设置内容,测量高度为,字体变小之前的高度
    android在Service中弹出Dialog对话框,即全局性对话框
    Could not find com.android.tools.build:gradle:3.0.0-alpha3
  • 原文地址:https://www.cnblogs.com/hongyu/p/2052084.html
Copyright © 2011-2022 走看看