2

私はコレクションを持っています

private ObservableCollection<ContentItemViewModel> _contentTree;
        public ObservableCollection<ContentItemViewModel> ContentTree
        {
            get { return _contentTree; }
        }

クラスContentItemViewModelには次のプロパティがあります。

 private string _published;
        public string Published
        {
            get
            {
                return _published;
            }
            set
            {
                _published = value;

                NotifyPropertyChanged("Published");
            }
        }

あれは - node.Published= Convert.ToDateTime(date.Value).ToString("dd MMM yyyy", new DateTimeFormatInfo());

ContentTreeコレクションを日付で並べ替える必要がありますか?これどうやってするの?

4

2 に答える 2

4

もっと良い方法があるかどうかはわかりませんが、次のことができます。ここでの考え方は、順序付きリストを作成し、順序付きリストの最初のforeachアイテムを作成し、コンテンツツリーから関連アイテムを削除して再度追加することです。

var tempList = _contentTree.OrderBy(p => DateTime.Parse(p.DateAndTime)); 

tempList.ToList().ForEach(q =>
            {
                _contentTree.Remove(q);
                _contentTree.Add(q);
            });

または、比較を使用できます。

Comparison<ContentItemViewModel> comparison = new Comparison<ContentItemViewModel>(
            (p,q) =>
            {
                DateTime first = DateTime.Parse(p.DateAndTime);
                DateTime second = DateTime.Parse(q.DateAndTime);
                if (first == second)
                    return 0;
                if (first > second)
                    return 1;
                return -1;
            });


        List<ContentItemViewModel> tempList = _contentTree.ToList();
        tempList.Sort(comparison);
        _contentTree = new ObservableCollection<ContentItemViewModel>(tempList);
于 2012-05-29T12:19:04.603 に答える
0

あなたはこれを使うことができます:

ConceptItems = new ObservableCollection<DataConcept>(ConceptItems.OrderBy(i => i.DateColumn));   
于 2018-09-25T07:19:20.493 に答える