SortHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Gtk;
  2. using System;
  3. namespace Ryujinx.Ui.Helper
  4. {
  5. static class SortHelper
  6. {
  7. public static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  8. {
  9. string aValue = model.GetValue(a, 5).ToString();
  10. string bValue = model.GetValue(b, 5).ToString();
  11. float aFloat;
  12. float bFloat;
  13. if (aValue.Length > 7 && aValue[^7..] == "minutes")
  14. {
  15. aValue = aValue.Replace("minutes", "");
  16. aFloat = (float.Parse(aValue) * 60);
  17. }
  18. else if (aValue.Length > 5 && aValue[^5..] == "hours")
  19. {
  20. aValue = aValue.Replace("hours", "");
  21. aFloat = (float.Parse(aValue) * 3600);
  22. }
  23. else if (aValue.Length > 4 && aValue[^4..] == "days")
  24. {
  25. aValue = aValue.Replace("days", "");
  26. aFloat = (float.Parse(aValue) * 86400);
  27. }
  28. else
  29. {
  30. aValue = aValue.Replace("seconds", "");
  31. aFloat = float.Parse(aValue);
  32. }
  33. if (bValue.Length > 7 && bValue[^7..] == "minutes")
  34. {
  35. bValue = bValue.Replace("minutes", "");
  36. bFloat = (float.Parse(bValue) * 60);
  37. }
  38. else if (bValue.Length > 5 && bValue[^5..] == "hours")
  39. {
  40. bValue = bValue.Replace("hours", "");
  41. bFloat = (float.Parse(bValue) * 3600);
  42. }
  43. else if (bValue.Length > 4 && bValue[^4..] == "days")
  44. {
  45. bValue = bValue.Replace("days", "");
  46. bFloat = (float.Parse(bValue) * 86400);
  47. }
  48. else
  49. {
  50. bValue = bValue[0..^8];
  51. bFloat = float.Parse(bValue);
  52. }
  53. if (aFloat > bFloat)
  54. {
  55. return -1;
  56. }
  57. else if (bFloat > aFloat)
  58. {
  59. return 1;
  60. }
  61. else
  62. {
  63. return 0;
  64. }
  65. }
  66. public static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  67. {
  68. string aValue = model.GetValue(a, 6).ToString();
  69. string bValue = model.GetValue(b, 6).ToString();
  70. if (aValue == "Never")
  71. {
  72. aValue = DateTime.UnixEpoch.ToString();
  73. }
  74. if (bValue == "Never")
  75. {
  76. bValue = DateTime.UnixEpoch.ToString();
  77. }
  78. return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
  79. }
  80. public static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
  81. {
  82. string aValue = model.GetValue(a, 8).ToString();
  83. string bValue = model.GetValue(b, 8).ToString();
  84. if (aValue[^3..] == "GiB")
  85. {
  86. aValue = (float.Parse(aValue[0..^3]) * 1024).ToString();
  87. }
  88. else
  89. {
  90. aValue = aValue[0..^3];
  91. }
  92. if (bValue[^3..] == "GiB")
  93. {
  94. bValue = (float.Parse(bValue[0..^3]) * 1024).ToString();
  95. }
  96. else
  97. {
  98. bValue = bValue[0..^3];
  99. }
  100. if (float.Parse(aValue) > float.Parse(bValue))
  101. {
  102. return -1;
  103. }
  104. else if (float.Parse(bValue) > float.Parse(aValue))
  105. {
  106. return 1;
  107. }
  108. else
  109. {
  110. return 0;
  111. }
  112. }
  113. }
  114. }