FileSizeSortComparer.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Ryujinx.Ui.App.Common;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Ava.Ui.Models.Generic
  4. {
  5. internal class FileSizeSortComparer : IComparer<ApplicationData>
  6. {
  7. public FileSizeSortComparer() { }
  8. public FileSizeSortComparer(bool isAscending) { _order = isAscending ? 1 : -1; }
  9. private int _order;
  10. public int Compare(ApplicationData x, ApplicationData y)
  11. {
  12. string aValue = x.FileSize;
  13. string bValue = y.FileSize;
  14. if (aValue[^3..] == "GiB")
  15. {
  16. aValue = (float.Parse(aValue[0..^3]) * 1024).ToString();
  17. }
  18. else
  19. {
  20. aValue = aValue[0..^3];
  21. }
  22. if (bValue[^3..] == "GiB")
  23. {
  24. bValue = (float.Parse(bValue[0..^3]) * 1024).ToString();
  25. }
  26. else
  27. {
  28. bValue = bValue[0..^3];
  29. }
  30. if (float.Parse(aValue) > float.Parse(bValue))
  31. {
  32. return -1 * _order;
  33. }
  34. else if (float.Parse(bValue) > float.Parse(aValue))
  35. {
  36. return 1 * _order;
  37. }
  38. else
  39. {
  40. return 0;
  41. }
  42. }
  43. }
  44. }