Wpf Mvvm sort ObservableCollection doesn't work

Anja 426 Reputation points
2021-04-28T09:36:35.983+00:00

Hi,
I have some ObservableCollection lists I'm adding and removing rows from.

When that is done (that works) I want to sort the list. This I do with this line

Categories_GetActive.OrderBy(o => o.CategoryName).ToList();

I have tried with and without discard. But it does nothing at all. No errors either. I have tried to see the output - it shows me the list without order anything.

I hope that one of you can tell me what to do for order the ObservableCollection

Best regards
Simsen :-)

    My ObservableCollection
    private ObservableCollection<Category> _categories_GetActive;
            public ObservableCollection<Category> Categories_GetActive
            {
                get
                {
                    return _categories_GetActive;
                }
                set
                {
                    _categories_GetActive = value;
                    OnPropertyChanged("Categories_GetActive");
                }
            }

My method where I'm trying to make a OrderBy

public void SaveChanges()
        {
            ResetMessages();
            if (SelectedCategory != null)
            {
                DalCategory dalCategory = new DalCategory();
                var categories = dalCategory.GetCategories();

                 Category category = SelectedCategory;

                dalCategory.SetCategory(SelectedCategory);

                bool oldIsObsolete = (bool)categories.First(i => i.CategoryId == SelectedCategory.CategoryId)?.CategoryIsObsolete;

                if (oldIsObsolete != SelectedCategory.CategoryIsObsolete)
                {
                    //Inaktiv
                    if (oldIsObsolete)
                    {
                        Categories_GetActive.Add(SelectedCategory);
                        Categories_GetActive.OrderBy(o => o.CategoryName).ToList();

                        Categories_GetInactive.Remove(SelectedCategory);
                    }
                    //Aktiv
                    else
                    {
                        Categories_GetInactive.Add(SelectedCategory);
                        _ = Categories_GetInactive.OrderBy(o => o.CategoryName).ToList();

                        Categories_GetActive.Remove(SelectedCategory);
                    }
                }                

                MessageOk = "Kategorien er gemt";                
            }
            else
            {
                MessageError = "Du skal vælge en kategori, før du kan gemme";
            }
        }
Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-04-28T11:07:35.383+00:00

    Hi Anja,
    you create a new sorted List (Categories_GetActive.OrderBy(o => o.CategoryName).ToList();) without assigning to _categories_GetActive. Please, try:

     this.Categories_GetActive = Categories_GetActive.OrderBy(o => o.CategoryName).ToList();
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.