title: MVVM Add Sort Description
body: <p>I want to add <code>Sort</code> functionality on top of two <code>Filters</code> that I have implemented.</p>
  
  <p>This is what my code looks like at the moment.</p>
  
  <p><code>XAML</code></p>
  
  <pre><code>&lt;Grid Grid.Row="1"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Border  BorderThickness="1" BorderBrush="Red"&gt;
                  &lt;ComboBox
                  Name="SortComboBox"
                  SelectionChanged="View_SelectionChanged"
                  ItemsSource="{Binding sortOptions, Mode=OneWay}" 
                  SelectedValue="{Binding selectedSortOption}"
              /&gt;
              &lt;/Border&gt;
              &lt;ComboBox
                  Name="GenreComboBox"
                  SelectionChanged="View_SelectionChanged"
                  ItemsSource="{Binding genreOptions, Mode=OneWay}" 
                  SelectedValue="{Binding selectedGenreOption}"
              /&gt;
              &lt;TextBox Name="SearchTextBox" Width="154" TextChanged="Search_SelectionChanged" Text="{Binding Path=searchTerm, UpdateSourceTrigger=PropertyChanged}" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;    
  </code></pre>
  
  <p>The first <code>SortComboBox</code> is used to <code>sort</code> movies while the <code>GenreComboBox</code> filters movies based on genre. The <code>SearchTextBox</code> is another filter to find movies by keywords.</p>
  
  <p><code>Behind Code</code></p>
  
  <pre><code>    private void View_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          _moviePanelVM.DisplayMovies.View.Refresh();
      }
  
      private void Search_SelectionChanged(object sender, TextChangedEventArgs e)
      {
          _moviePanelVM.DisplayMovies.View.Refresh();
      }
  </code></pre>
  
  <p><code>ViewModel</code></p>
  
  <pre><code> public MoviePanelViewModel()
      {
          ...
          ...
  
          this.DisplayMovies = new CollectionViewSource();
          this.DisplayMovies.Source = this.Movies;
          this.DisplayMovies.Filter += GenreFilter;
          this.DisplayMovies.Filter += SearchFilter;
      }
  
      private void GenreFilter(object sender, FilterEventArgs e)
      {
          MediaDetail media = e.Item as MediaDetail;
          if (selectedGenreOption != "All" &amp;&amp; !media.genre.Contains(selectedGenreOption))
              e.Accepted = false;    
      }
  
      private void SearchFilter(object sender, FilterEventArgs e)
      {
          MediaDetail media = e.Item as MediaDetail;
          if (!media.title.ToLower().Contains(searchTerm.ToLower()))
              e.Accepted = false;
      }
  </code></pre>
  
  <p>The <code>SortComboBox</code> may have a selected value of <code>A-Z</code> in which case I would <code>sort</code> a particular way. Or it may have a value of <code>Z-A</code> in which case I would <code>sort</code> it another way. </p>
  
  <p>My question is, where should I be adding <code>SortDescriptions</code> and the logic to control what kind of sort should occur in order to ensure that the MVVM pattern is maintained? </p>
  
  <p><code>UPDATE</code></p>
  
  <p><code>Code Behind</code></p>
  
  <pre><code>private void Sort_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          _moviePanelVM.SortMovies();
      }
  </code></pre>
  
  <p>Changed my <code>XAML</code> to:</p>
  
  <pre><code>&lt;ComboBox
     Name="SortComboBox"
     SelectionChanged="Sort_SelectionChanged"
     ItemsSource="{Binding sortOptions, Mode=OneWay}" 
     SelectedValue="{Binding selectedSortOption}"
  /&gt;
  </code></pre>
  
  <p><code>ViewModel</code></p>
  
  <pre><code>public void SortMovies()
      {
          DisplayMovies.SortDescriptions.Clear();
  
          switch (SelectedSortOption)
          {
              case "A-Z":
                  DisplayMovies.SortDescriptions.Add(new SortDescription("title", ListSortDirection.Ascending)); break;
              case "Z-A":
                  DisplayMovies.SortDescriptions.Add(new SortDescription("title", ListSortDirection.Descending)); break;
              case "Release Date":
                  DisplayMovies.SortDescriptions.Add(new SortDescription("year", ListSortDirection.Descending)); break;
              case "Rating":
                  DisplayMovies.SortDescriptions.Add(new SortDescription("rating", ListSortDirection.Descending)); break;
          }
      }
  </code></pre>
  
  <p>This works but I was wondering if I should be doing it like this? As the <code>filters</code> were simply <code>Refreshing</code> the <code>view</code> but with <code>sort</code> I'm calling a function in the <code>ViewModel</code>.</p>