AppListFavoriteComparable.cs
1 using Ryujinx.UI.App.Common; 2 using System; 3 4 namespace Ryujinx.Ava.UI.ViewModels 5 { 6 /// <summary> 7 /// Implements a custom comparer which is used for sorting titles by favorite on a UI. 8 /// Returns a sorted list of favorites in alphabetical order, followed by all non-favorites sorted alphabetical. 9 /// </summary> 10 public readonly struct AppListFavoriteComparable : IComparable 11 { 12 /// <summary> 13 /// The application data being compared. 14 /// </summary> 15 private readonly ApplicationData app; 16 17 /// <summary> 18 /// Constructs a new <see cref="AppListFavoriteComparable"/> with the specified application data. 19 /// </summary> 20 /// <param name="app">The app data being compared.</param> 21 public AppListFavoriteComparable(ApplicationData app) 22 { 23 ArgumentNullException.ThrowIfNull(app, nameof(app)); 24 this.app = app; 25 } 26 27 /// <inheritdoc/> 28 public readonly int CompareTo(object o) 29 { 30 if (o is AppListFavoriteComparable other) 31 { 32 if (app.Favorite == other.app.Favorite) 33 { 34 return string.Compare(app.Name, other.app.Name, StringComparison.OrdinalIgnoreCase); 35 } 36 37 return app.Favorite ? -1 : 1; 38 } 39 40 throw new InvalidCastException($"Cannot cast {o.GetType()} to {nameof(AppListFavoriteComparable)}"); 41 } 42 } 43 }