Stable sorting of ResultCollection (#5850)

* Stable sorting of ResultCollection

* nit fixes for resultsViewModel and ResultCollection
This commit is contained in:
Divyansh Srivastava 2020-08-11 10:24:56 -07:00 committed by GitHub
parent ac10c988b9
commit 2d2cb22806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 30 deletions

View File

@ -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<ResultViewModel>, INotifyCollectionChanged
{
public ResultCollection() { }
public ResultCollection(IEnumerable<ResultViewModel> 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;
}
}
/// <summary>
/// sort the list in descending order of score
/// </summary>
public new void Sort()
{
base.Sort(CompareResultViewModel);
}
/// <summary>
/// Notify change in the List view items
/// </summary>

View File

@ -502,7 +502,7 @@ namespace PowerLauncher.ViewModel
}
currentCancellationToken.ThrowIfCancellationRequested();
Results.Results.Sort();
Results.Sort();
}
}

View File

@ -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