From 2d2cb228065cde2f4f8bea423bf5b289d3bd89c0 Mon Sep 17 00:00:00 2001 From: Divyansh Srivastava Date: Tue, 11 Aug 2020 10:24:56 -0700 Subject: [PATCH] Stable sorting of ResultCollection (#5850) * Stable sorting of ResultCollection * nit fixes for resultsViewModel and ResultCollection --- .../PowerLauncher/Helper/ResultCollection.cs | 29 ++++--------------- .../PowerLauncher/ViewModel/MainViewModel.cs | 2 +- .../ViewModel/ResultsViewModel.cs | 19 ++++++++---- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/modules/launcher/PowerLauncher/Helper/ResultCollection.cs b/src/modules/launcher/PowerLauncher/Helper/ResultCollection.cs index 83a80f1385..3d70f47c76 100644 --- a/src/modules/launcher/PowerLauncher/Helper/ResultCollection.cs +++ b/src/modules/launcher/PowerLauncher/Helper/ResultCollection.cs @@ -4,38 +4,19 @@ using System.Collections.Generic; using System.Collections.Specialized; +using System.Linq; using PowerLauncher.ViewModel; namespace PowerLauncher.Helper { public class ResultCollection : List, INotifyCollectionChanged { + public ResultCollection() { } + + public ResultCollection(IEnumerable rvm) : base(rvm) { } + public event NotifyCollectionChangedEventHandler CollectionChanged; - private int CompareResultViewModel(ResultViewModel c1, ResultViewModel c2) - { - if (c1.Result.Score > c2.Result.Score) - { - return -1; - } - else if (c1.Result.Score == c2.Result.Score) - { - return 0; - } - else - { - return 1; - } - } - - /// - /// sort the list in descending order of score - /// - public new void Sort() - { - base.Sort(CompareResultViewModel); - } - /// /// Notify change in the List view items /// diff --git a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs index 208e62fb28..618dfff712 100644 --- a/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs +++ b/src/modules/launcher/PowerLauncher/ViewModel/MainViewModel.cs @@ -502,7 +502,7 @@ namespace PowerLauncher.ViewModel } currentCancellationToken.ThrowIfCancellationRequested(); - Results.Results.Sort(); + Results.Sort(); } } diff --git a/src/modules/launcher/PowerLauncher/ViewModel/ResultsViewModel.cs b/src/modules/launcher/PowerLauncher/ViewModel/ResultsViewModel.cs index 568ae44638..1873b3ffe5 100644 --- a/src/modules/launcher/PowerLauncher/ViewModel/ResultsViewModel.cs +++ b/src/modules/launcher/PowerLauncher/ViewModel/ResultsViewModel.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Windows; using System.Windows.Controls; @@ -19,12 +20,12 @@ namespace PowerLauncher.ViewModel { #region Private Fields - public ResultCollection Results { get; } - private readonly object _collectionLock = new object(); + private readonly Settings _settings; - // private int MaxResults => _settings?.MaxResultsToShow ?? 6; + #endregion + public ResultsViewModel() { Results = new ResultCollection(); @@ -46,8 +47,6 @@ namespace PowerLauncher.ViewModel }; } - #endregion - #region Properties public int MaxHeight @@ -90,6 +89,8 @@ namespace PowerLauncher.ViewModel public Visibility Visibility { get; set; } = Visibility.Hidden; + public ResultCollection Results { get; } + #endregion #region Private Methods @@ -238,6 +239,14 @@ namespace PowerLauncher.ViewModel Results.AddRange(newResults); } + + public void Sort() + { + var sorted = Results.OrderByDescending(x => x.Result.Score).ToList(); + Clear(); + Results.AddRange(sorted); + } + #endregion #region FormattedText Dependency Property