aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Models/Generic/FileSizeSortComparer.cs
blob: 08b1754c2bafbf3948ce3323279f8befdb5638bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Ryujinx.Ui.App.Common;
using System.Collections.Generic;

namespace Ryujinx.Ava.Ui.Models.Generic
{
    internal class FileSizeSortComparer : IComparer<ApplicationData>
    {
        public FileSizeSortComparer() { }
        public FileSizeSortComparer(bool isAscending) { _order = isAscending ? 1 : -1; }

        private int _order;

        public int Compare(ApplicationData x, ApplicationData y)
        {
            string aValue = x.FileSize;
            string bValue = y.FileSize;

            if (aValue[^3..] == "GiB")
            {
                aValue = (float.Parse(aValue[0..^3]) * 1024).ToString();
            }
            else
            {
                aValue = aValue[0..^3];
            }

            if (bValue[^3..] == "GiB")
            {
                bValue = (float.Parse(bValue[0..^3]) * 1024).ToString();
            }
            else
            {
                bValue = bValue[0..^3];
            }

            if (float.Parse(aValue) > float.Parse(bValue))
            {
                return -1 * _order;
            }
            else if (float.Parse(bValue) > float.Parse(aValue))
            {
                return 1 * _order;
            }
            else
            {
                return 0;
            }
        }
    }
}