Create and use IEnumerable.OrderByInDir extension

This commit is contained in:
YoshiRulz 2019-03-27 15:57:49 +10:00
parent f060bc1211
commit 2b7443bd83
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 75 additions and 315 deletions

View File

@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Text;
using BizHawk.Common.CollectionExtensions;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
@ -511,199 +512,57 @@ namespace BizHawk.Client.Common
return true;
}
public void Sort(string column, bool reverse)
private IOrderedEnumerable<Cheat> GetSorted(string column, bool reverse)
{
switch (column)
{
case NameColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Name)
.ThenBy(c => c.Address ?? 0);
case AddressColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Address ?? 0)
.ThenBy(c => c.Name)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Address ?? 0)
.ThenBy(c => c.Name)
.ToList();
}
break;
default:
return _cheatList.OrderByInDir(reverse, c => c.Address ?? 0)
.ThenBy(c => c.Name);
case ValueColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Value ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Value ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Value ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case CompareColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Compare ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Compare ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Compare ?? 0)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case OnColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Enabled)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Enabled)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Enabled)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case DomainColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Domain)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Domain)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Domain)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case SizeColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => ((int)c.Size))
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => ((int)c.Size))
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => (int) c.Size)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case EndianColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.BigEndian)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.BigEndian)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.BigEndian)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case TypeColumn:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.Type)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.Type)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.Type)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
case ComparisonType:
if (reverse)
{
_cheatList = _cheatList
.OrderByDescending(c => c.ComparisonType)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
else
{
_cheatList = _cheatList
.OrderBy(c => c.ComparisonType)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0)
.ToList();
}
break;
return _cheatList.OrderByInDir(reverse, c => c.ComparisonType)
.ThenBy(c => c.Name)
.ThenBy(c => c.Address ?? 0);
}
}
public void Sort(string column, bool reverse)
{
_cheatList = GetSorted(column, reverse).ToList();
}
public void SetDefaultFileName(string defaultFileName)
{
_defaultFileName = defaultFileName;

View File

@ -426,54 +426,21 @@ namespace BizHawk.Client.Common
switch (column)
{
case WatchList.ADDRESS:
if (reverse)
{
_watchList = _watchList.OrderByDescending(w => w.Address).ToList();
}
else
{
_watchList = _watchList.OrderBy(w => w.Address).ToList();
_isSorted = true;
}
_watchList = _watchList.OrderByInDir(reverse, w => w.Address).ToList();
if (!reverse) _isSorted = true;
break;
case WatchList.VALUE:
_watchList = reverse
? _watchList.OrderByDescending(w => GetValue(w.Address)).ToList()
: _watchList.OrderBy(w => GetValue(w.Address)).ToList();
_watchList = _watchList.OrderByInDir(reverse, w => GetValue(w.Address)).ToList();
break;
case WatchList.PREV:
_watchList = reverse
? _watchList.OrderByDescending(w => w.Previous).ToList()
: _watchList.OrderBy(w => w.Previous).ToList();
_watchList = _watchList.OrderByInDir(reverse, w => w.Previous).ToList();
break;
case WatchList.CHANGES:
if (_settings.Mode == Settings.SearchMode.Detailed)
{
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.OrderByInDir(reverse, w => ((IMiniWatchDetails) w).ChangeCount).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.OrderByInDir(reverse, w => GetValue(w.Address) - w.Previous).ToList();
break;
}
}

View File

@ -497,34 +497,14 @@ namespace BizHawk.Client.EmuHawk
{
// Header, Value
case "Header":
if (_sortDetailsReverse)
{
detailsList = detailsList
.OrderByDescending(x => x.Keys)
.ThenBy(x => x.Values).ToList();
}
else
{
detailsList = detailsList
.OrderBy(x => x.Keys)
.ThenBy(x => x.Values).ToList();
}
detailsList = detailsList.OrderByInDir(_sortDetailsReverse, x => x.Keys)
.ThenBy(x => x.Values)
.ToList();
break;
case "Value":
if (_sortDetailsReverse)
{
detailsList = detailsList
.OrderByDescending(x => x.Values)
.ThenBy(x => x.Keys).ToList();
}
else
{
detailsList = detailsList
.OrderBy(x => x.Values)
.ThenBy(x => x.Keys).ToList();
}
detailsList = detailsList.OrderByInDir(_sortDetailsReverse, x => x.Values)
.ThenBy(x => x.Keys)
.ToList();
break;
}

View File

@ -1333,14 +1333,10 @@ namespace BizHawk.Client.EmuHawk
switch (columnToSort)
{
case "Script":
luaListTemp = _sortReverse
? luaListTemp.OrderByDescending(lf => lf.Name).ThenBy(lf => lf.Path).ToList()
: luaListTemp.OrderBy(lf => lf.Name).ThenBy(lf => lf.Path).ToList();
luaListTemp = luaListTemp.OrderByInDir(_sortReverse, lf => lf.Name).ThenBy(lf => lf.Path).ToList();
break;
case "Path":
luaListTemp = _sortReverse
? luaListTemp.OrderByDescending(lf => lf.Path).ThenBy(lf => lf.Name).ToList()
: luaListTemp.OrderBy(lf => lf.Path).ThenBy(lf => lf.Name).ToList();
luaListTemp = luaListTemp.OrderByInDir(_sortReverse, lf => lf.Path).ThenBy(lf => lf.Name).ToList();
break;
}

View File

@ -93,49 +93,24 @@ namespace BizHawk.Client.EmuHawk
private void OrderColumn(int column)
{
_columnSort.Column = column;
if (_columnSort.Descending)
switch (column)
{
switch (column)
{
case 0: // Return
FunctionList = FunctionList.OrderByDescending(x => x.ReturnType).ToList();
break;
case 1: // Library
FunctionList = FunctionList.OrderByDescending(x => x.Library).ToList();
break;
case 2: // Name
FunctionList = FunctionList.OrderByDescending(x => x.Name).ToList();
break;
case 3: // Parameters
FunctionList = FunctionList.OrderByDescending(x => x.ParameterList).ToList();
break;
case 4: // Description
FunctionList = FunctionList.OrderByDescending(x => x.Description).ToList();
break;
}
case 0: // Return
FunctionList = FunctionList.OrderByInDir(_columnSort.Descending, x => x.ReturnType).ToList();
break;
case 1: // Library
FunctionList = FunctionList.OrderByInDir(_columnSort.Descending, x => x.Library).ToList();
break;
case 2: // Name
FunctionList = FunctionList.OrderByInDir(_columnSort.Descending, x => x.Name).ToList();
break;
case 3: // Parameters
FunctionList = FunctionList.OrderByInDir(_columnSort.Descending, x => x.ParameterList).ToList();
break;
case 4: // Description
FunctionList = FunctionList.OrderByInDir(_columnSort.Descending, x => x.Description).ToList();
break;
}
else
{
switch (column)
{
case 0: // Return
FunctionList = FunctionList.OrderBy(x => x.ReturnType).ToList();
break;
case 1: // Library
FunctionList = FunctionList.OrderBy(x => x.Library).ToList();
break;
case 2: // Name
FunctionList = FunctionList.OrderBy(x => x.Name).ToList();
break;
case 3: // Parameters
FunctionList = FunctionList.OrderBy(x => x.ParameterList).ToList();
break;
case 4: // Description
FunctionList = FunctionList.OrderBy(x => x.Description).ToList();
break;
}
}
UpdateList();
}

View File

@ -486,34 +486,14 @@ namespace BizHawk.Client.MultiHawk
{
// Header, Value
case "Header":
if (_sortDetailsReverse)
{
detailsList = detailsList
.OrderByDescending(x => x.Keys)
.ThenBy(x => x.Values).ToList();
}
else
{
detailsList = detailsList
.OrderBy(x => x.Keys)
.ThenBy(x => x.Values).ToList();
}
detailsList = detailsList.OrderByInDir(_sortDetailsReverse, x => x.Keys)
.ThenBy(x => x.Values)
.ToList();
break;
case "Value":
if (_sortDetailsReverse)
{
detailsList = detailsList
.OrderByDescending(x => x.Values)
.ThenBy(x => x.Keys).ToList();
}
else
{
detailsList = detailsList
.OrderBy(x => x.Values)
.ThenBy(x => x.Keys).ToList();
}
detailsList = detailsList.OrderByInDir(_sortDetailsReverse, x => x.Values)
.ThenBy(x => x.Keys)
.ToList();
break;
}

View File

@ -117,5 +117,8 @@ namespace BizHawk.Common.CollectionExtensions
}
public static IEnumerable<T> SelectAsIndexOf<T>(this IEnumerable<int> indices, IList<T> list) => indices.Select(i => list[i]);
public static IOrderedEnumerable<T> OrderByInDir<T, S>(this IEnumerable<T> list, bool desc, Func<T, S> keySelector) =>
desc ? list.OrderByDescending(keySelector) : list.OrderBy(keySelector);
}
}