diff --git a/BizHawk.Client.Common/tools/CheatList.cs b/BizHawk.Client.Common/tools/CheatList.cs index 5c69ba29f7..beb3761082 100644 --- a/BizHawk.Client.Common/tools/CheatList.cs +++ b/BizHawk.Client.Common/tools/CheatList.cs @@ -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 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; diff --git a/BizHawk.Client.Common/tools/RamSearchEngine.cs b/BizHawk.Client.Common/tools/RamSearchEngine.cs index 8191349a66..8b7b18a3dd 100644 --- a/BizHawk.Client.Common/tools/RamSearchEngine.cs +++ b/BizHawk.Client.Common/tools/RamSearchEngine.cs @@ -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() - .OrderByDescending(w => w.ChangeCount) - .Cast().ToList(); - } - else - { - _watchList = _watchList - .Cast() - .OrderBy(w => w.ChangeCount) - .Cast().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; } } diff --git a/BizHawk.Client.EmuHawk/movie/PlayMovie.cs b/BizHawk.Client.EmuHawk/movie/PlayMovie.cs index c223f39631..6dbd2db07f 100644 --- a/BizHawk.Client.EmuHawk/movie/PlayMovie.cs +++ b/BizHawk.Client.EmuHawk/movie/PlayMovie.cs @@ -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; } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 481b0861e0..537a3b41db 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -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; } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs index 7181ff87c9..4c353a2c23 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaFunctionsForm.cs @@ -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(); } diff --git a/BizHawk.Client.MultiHawk/movie/PlayMovie.cs b/BizHawk.Client.MultiHawk/movie/PlayMovie.cs index 5c3aba3c17..746ad67756 100644 --- a/BizHawk.Client.MultiHawk/movie/PlayMovie.cs +++ b/BizHawk.Client.MultiHawk/movie/PlayMovie.cs @@ -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; } diff --git a/BizHawk.Common/Extensions/CollectionExtensions.cs b/BizHawk.Common/Extensions/CollectionExtensions.cs index ecb20727bd..97d6e9bb3c 100644 --- a/BizHawk.Common/Extensions/CollectionExtensions.cs +++ b/BizHawk.Common/Extensions/CollectionExtensions.cs @@ -117,5 +117,8 @@ namespace BizHawk.Common.CollectionExtensions } public static IEnumerable SelectAsIndexOf(this IEnumerable indices, IList list) => indices.Select(i => list[i]); + + public static IOrderedEnumerable OrderByInDir(this IEnumerable list, bool desc, Func keySelector) => + desc ? list.OrderByDescending(keySelector) : list.OrderBy(keySelector); } }