TimePlayedSortComparer.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Ryujinx.Ui.App.Common;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Ava.Ui.Models.Generic
  4. {
  5. internal class TimePlayedSortComparer : IComparer<ApplicationData>
  6. {
  7. public TimePlayedSortComparer() { }
  8. public TimePlayedSortComparer(bool isAscending) { _order = isAscending ? 1 : -1; }
  9. private int _order;
  10. public int Compare(ApplicationData x, ApplicationData y)
  11. {
  12. string aValue = x.TimePlayed;
  13. string bValue = y.TimePlayed;
  14. if (aValue.Length > 4 && aValue[^4..] == "mins")
  15. {
  16. aValue = (float.Parse(aValue[0..^5]) * 60).ToString();
  17. }
  18. else if (aValue.Length > 3 && aValue[^3..] == "hrs")
  19. {
  20. aValue = (float.Parse(aValue[0..^4]) * 3600).ToString();
  21. }
  22. else if (aValue.Length > 4 && aValue[^4..] == "days")
  23. {
  24. aValue = (float.Parse(aValue[0..^5]) * 86400).ToString();
  25. }
  26. else
  27. {
  28. aValue = aValue[0..^1];
  29. }
  30. if (bValue.Length > 4 && bValue[^4..] == "mins")
  31. {
  32. bValue = (float.Parse(bValue[0..^5]) * 60).ToString();
  33. }
  34. else if (bValue.Length > 3 && bValue[^3..] == "hrs")
  35. {
  36. bValue = (float.Parse(bValue[0..^4]) * 3600).ToString();
  37. }
  38. else if (bValue.Length > 4 && bValue[^4..] == "days")
  39. {
  40. bValue = (float.Parse(bValue[0..^5]) * 86400).ToString();
  41. }
  42. else
  43. {
  44. bValue = bValue[0..^1];
  45. }
  46. if (float.Parse(aValue) > float.Parse(bValue))
  47. {
  48. return -1 * _order;
  49. }
  50. else if (float.Parse(bValue) > float.Parse(aValue))
  51. {
  52. return 1 * _order;
  53. }
  54. else
  55. {
  56. return 0;
  57. }
  58. }
  59. }
  60. }