create an OrderBy() overload that does OrderBy or OrderByDesc, and use it to simplify sorting in RamSearchEngine (can be used to simplify other things like CheatList)

This commit is contained in:
adelikat 2020-03-04 18:40:26 -06:00
parent d6e56f8aa8
commit 888a5225fe
2 changed files with 18 additions and 32 deletions

View File

@ -313,54 +313,31 @@ namespace BizHawk.Client.Common.RamSearchEngine
switch (column)
{
case WatchList.ADDRESS:
if (reverse)
if (!reverse)
{
_watchList = _watchList.OrderByDescending(w => w.Address).ToList();
}
else
{
_watchList = _watchList.OrderBy(w => w.Address).ToList();
_isSorted = true;
}
_watchList = _watchList.OrderBy(w => w.Address, reverse).ToList();
break;
case WatchList.VALUE:
_watchList = reverse
? _watchList.OrderByDescending(w => GetValue(w.Address)).ToList()
: _watchList.OrderBy(w => GetValue(w.Address)).ToList();
_watchList = _watchList.OrderBy(w => GetValue(w.Address), reverse).ToList();
break;
case WatchList.PREV:
_watchList = reverse
? _watchList.OrderByDescending(w => w.Previous).ToList()
: _watchList.OrderBy(w => w.Previous).ToList();
_watchList = _watchList.OrderBy(w => w.Previous, reverse).ToList();
break;
case WatchList.CHANGES:
if (_settings.IsDetailed())
{
if (reverse)
{
_watchList = _watchList
.Cast<IMiniWatchDetails>()
.OrderByDescending(w => w.ChangeCount)
.Cast<IMiniWatch>().ToList();
}
else
{
_watchList = _watchList
.Cast<IMiniWatchDetails>()
.OrderBy(w => w.ChangeCount)
.Cast<IMiniWatch>().ToList();
}
_watchList = _watchList
.Cast<IMiniWatchDetails>()
.OrderBy(w => w.ChangeCount, reverse)
.Cast<IMiniWatch>().ToList();
}
break;
case WatchList.DIFF:
_watchList = reverse
? _watchList.OrderByDescending(w => GetValue(w.Address) - w.Previous).ToList()
: _watchList.OrderBy(w => GetValue(w.Address) - w.Previous).ToList();
_watchList = _watchList.OrderBy(w => GetValue(w.Address) - w.Previous, reverse).ToList();
break;
}
}

View File

@ -2,11 +2,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace BizHawk.Common.CollectionExtensions
{
public static class CollectionExtensions
{
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
bool desc)
{
return desc ? source.OrderByDescending(keySelector) : source.OrderBy(keySelector);
}
public static int LowerBoundBinarySearch<T, TKey>(this IList<T> list, Func<T, TKey> keySelector, TKey key) where TKey : IComparable<TKey>
{
int min = 0;