aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Models/TimePlayedSortComparer.cs
blob: 3613c8c7cacbc47911526baf3ceb3ebac34aea46 (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
using Ryujinx.Ui.App.Common;
using System.Collections;

namespace Ryujinx.Ava.Ui.Models
{
    internal class TimePlayedSortComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            string aValue = (x as ApplicationData).TimePlayed;
            string bValue = (y as ApplicationData).TimePlayed;

            if (aValue.Length > 4 && aValue[^4..] == "mins")
            {
                aValue = (float.Parse(aValue[0..^5]) * 60).ToString();
            }
            else if (aValue.Length > 3 && aValue[^3..] == "hrs")
            {
                aValue = (float.Parse(aValue[0..^4]) * 3600).ToString();
            }
            else if (aValue.Length > 4 && aValue[^4..] == "days")
            {
                aValue = (float.Parse(aValue[0..^5]) * 86400).ToString();
            }
            else
            {
                aValue = aValue[0..^1];
            }

            if (bValue.Length > 4 && bValue[^4..] == "mins")
            {
                bValue = (float.Parse(bValue[0..^5]) * 60).ToString();
            }
            else if (bValue.Length > 3 && bValue[^3..] == "hrs")
            {
                bValue = (float.Parse(bValue[0..^4]) * 3600).ToString();
            }
            else if (bValue.Length > 4 && bValue[^4..] == "days")
            {
                bValue = (float.Parse(bValue[0..^5]) * 86400).ToString();
            }
            else
            {
                bValue = bValue[0..^1];
            }

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