B4J Question (Solved)Sort by time and date?

G-ShadoW

Active Member
Licensed User
Longtime User
Hello, need help...
How to sort it by time and date?

List look's like this

B4X:
OPZJN7-RNVKA-GTVHN7 - 01:13:25 - 12.02.2022
OZJE4I-GY2VI-JZR2D5 - 19:17:24 - 17.02.2022
OY4GIP-Y2YPE-3G6E4M - 14:01:47 - 12.02.2022
OA7TGH-KHPDE-TIIZ5C - 14:05:56 - 12.02.2022
OKLODM-UHBJA-TNUOTH - 22:57:14 - 11.02.2022
ORBRR4-ZBULM-U2WSCR - 17:37:48 - 13.02.2022
OX6FPA-NJR4D-UGYKPI - 23:48:52 - 02.03.2022
OXOB4J-DTSMQ-A2LEDF - 00:12:52 - 12.02.2022
OBNHPX-BMCOC-BGL25O - 11:25:44 - 13.02.2022
OOKP6V-4OGDC-E7BHHX - 23:31:14 - 11.02.2022
OK3IOZ-TIJHB-COI45B - 09:25:53 - 05.04.2022
OOMJAO-6BICL-4FEVZ4 - 22:47:30 - 11.02.2022

B4X:
Dim s As Long
            s = DateTime.Now
            Log("Operation sort MAP started...")
            
                For i = 0 To closed.Size - 1
            
                DateTime.DateFormat = "dd.MM.yyyy"
                DateTime.TimeFormat = "HH:mm:ss"
                
                        Dim time As String
                    If closed.GetValueAt(i).As(Map).ContainsKey("opentm") Then
                        time = closed.GetValueAt(i).As(Map).Get("opentm")
                        time = time.Replace(".","")
                        time = time.SubString2(0,10)
                    Dim unixtime As Long = time
                    Dim targetDate As Long = DateUtils.UnixTimeToTicks(unixtime)
                    Log(closed.GetKeyAt(i) & " - " & DateTime.Time(targetDate) & " - " & DateTime.Date(targetDate))
                    End If
                Next
            Log("Operation took: " & (DateTime.Now - s) & " ms")
 

DonManfred

Expert
Licensed User
Longtime User
Rearrange the lines. Extract date and time to calculate a sortable date.

Make
B4X:
20220213173748 ORBRR4-ZBULM-U2WSCR
from the line
B4X:
ORBRR4-ZBULM-U2WSCR - 17:37:48 - 13.02.2022
You can then use the List.Sort method...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Professionals do not use Map.GetKeyAt / GetValueAt: https://www.b4x.com/android/forum/t...ommon-mistakes-and-other-tips.116651/#content

B4X:
Sub Process_Globals
    Type MyItem (Text As String, Date As Long)
End Sub

Sub AppStart (Args() As String)
    Dim s As String = $"OPZJN7-RNVKA-GTVHN7 - 01:13:25 - 12.02.2022
OZJE4I-GY2VI-JZR2D5 - 19:17:24 - 17.02.2022
OY4GIP-Y2YPE-3G6E4M - 14:01:47 - 12.02.2022
OA7TGH-KHPDE-TIIZ5C - 14:05:56 - 12.02.2022
OKLODM-UHBJA-TNUOTH - 22:57:14 - 11.02.2022
ORBRR4-ZBULM-U2WSCR - 17:37:48 - 13.02.2022
OX6FPA-NJR4D-UGYKPI - 23:48:52 - 02.03.2022
OXOB4J-DTSMQ-A2LEDF - 00:12:52 - 12.02.2022
OBNHPX-BMCOC-BGL25O - 11:25:44 - 13.02.2022
OOKP6V-4OGDC-E7BHHX - 23:31:14 - 11.02.2022
OK3IOZ-TIJHB-COI45B - 09:25:53 - 05.04.2022
OOMJAO-6BICL-4FEVZ4 - 22:47:30 - 11.02.2022"$
    DateTime.DateFormat = "HH:mm:ss - dd.MM.yyyy"
    Dim lines() As String  = Regex.Split("\n", s)
    Dim items As List
    items.Initialize
    For Each line As String In lines
        items.Add(CreateMyItem(line.SubString2(0, line.IndexOf(" ")), DateTime.DateParse(line.SubString(line.IndexOf(" - ") + 2))))
    Next
    items.SortType("Date", True)
    'test
    For Each item As MyItem In items
        Log($"${item.Text}: $Date{item.Date}"$)
    Next
End Sub


Public Sub CreateMyItem (Text As String, Date As Long) As MyItem
    Dim t1 As MyItem
    t1.Initialize
    t1.Text = Text
    t1.Date = Date
    Return t1
End Sub
 
Upvote 0

G-ShadoW

Active Member
Licensed User
Longtime User
Professionals do not use Map.GetKeyAt / GetValueAt: https://www.b4x.com/android/forum/t...ommon-mistakes-and-other-tips.116651/#content

B4X:
Sub Process_Globals
    Type MyItem (Text As String, Date As Long)
End Sub

Sub AppStart (Args() As String)
    Dim s As String = $"OPZJN7-RNVKA-GTVHN7 - 01:13:25 - 12.02.2022
OZJE4I-GY2VI-JZR2D5 - 19:17:24 - 17.02.2022
OY4GIP-Y2YPE-3G6E4M - 14:01:47 - 12.02.2022
OA7TGH-KHPDE-TIIZ5C - 14:05:56 - 12.02.2022
OKLODM-UHBJA-TNUOTH - 22:57:14 - 11.02.2022
ORBRR4-ZBULM-U2WSCR - 17:37:48 - 13.02.2022
OX6FPA-NJR4D-UGYKPI - 23:48:52 - 02.03.2022
OXOB4J-DTSMQ-A2LEDF - 00:12:52 - 12.02.2022
OBNHPX-BMCOC-BGL25O - 11:25:44 - 13.02.2022
OOKP6V-4OGDC-E7BHHX - 23:31:14 - 11.02.2022
OK3IOZ-TIJHB-COI45B - 09:25:53 - 05.04.2022
OOMJAO-6BICL-4FEVZ4 - 22:47:30 - 11.02.2022"$
    DateTime.DateFormat = "HH:mm:ss - dd.MM.yyyy"
    Dim lines() As String  = Regex.Split("\n", s)
    Dim items As List
    items.Initialize
    For Each line As String In lines
        items.Add(CreateMyItem(line.SubString2(0, line.IndexOf(" ")), DateTime.DateParse(line.SubString(line.IndexOf(" - ") + 2))))
    Next
    items.SortType("Date", True)
    'test
    For Each item As MyItem In items
        Log($"${item.Text}: $Date{item.Date}"$)
    Next
End Sub


Public Sub CreateMyItem (Text As String, Date As Long) As MyItem
    Dim t1 As MyItem
    t1.Initialize
    t1.Text = Text
    t1.Date = Date
    Return t1
End Sub

I'm not proffesional btw, thank you for your help.

The reason why I have used Map.GetKeyAt and GetValueAt is JSON file with too many parameters...
 

Attachments

  • JSON.txt
    73.8 KB · Views: 104
Upvote 0

Magma

Expert
Licensed User
Longtime User
Professionals do not use Map.GetKeyAt / GetValueAt: https://www.b4x.com/android/forum/t...ommon-mistakes-and-other-tips.116651/#content

B4X:
Sub Process_Globals
    Type MyItem (Text As String, Date As Long)
End Sub

Sub AppStart (Args() As String)
    Dim s As String = $"OPZJN7-RNVKA-GTVHN7 - 01:13:25 - 12.02.2022
OZJE4I-GY2VI-JZR2D5 - 19:17:24 - 17.02.2022
OY4GIP-Y2YPE-3G6E4M - 14:01:47 - 12.02.2022
OA7TGH-KHPDE-TIIZ5C - 14:05:56 - 12.02.2022
OKLODM-UHBJA-TNUOTH - 22:57:14 - 11.02.2022
ORBRR4-ZBULM-U2WSCR - 17:37:48 - 13.02.2022
OX6FPA-NJR4D-UGYKPI - 23:48:52 - 02.03.2022
OXOB4J-DTSMQ-A2LEDF - 00:12:52 - 12.02.2022
OBNHPX-BMCOC-BGL25O - 11:25:44 - 13.02.2022
OOKP6V-4OGDC-E7BHHX - 23:31:14 - 11.02.2022
OK3IOZ-TIJHB-COI45B - 09:25:53 - 05.04.2022
OOMJAO-6BICL-4FEVZ4 - 22:47:30 - 11.02.2022"$
    DateTime.DateFormat = "HH:mm:ss - dd.MM.yyyy"
    Dim lines() As String  = Regex.Split("\n", s)
    Dim items As List
    items.Initialize
    For Each line As String In lines
        items.Add(CreateMyItem(line.SubString2(0, line.IndexOf(" ")), DateTime.DateParse(line.SubString(line.IndexOf(" - ") + 2))))
    Next
    items.SortType("Date", True)
    'test
    For Each item As MyItem In items
        Log($"${item.Text}: $Date{item.Date}"$)
    Next
End Sub


Public Sub CreateMyItem (Text As String, Date As Long) As MyItem
    Dim t1 As MyItem
    t1.Initialize
    t1.Text = Text
    t1.Date = Date
    Return t1
End Sub
@Erel Wow... this is a total different way... you are programming - as you talking/reading-speaking (any verb with ing) :)
 
Upvote 0

G-ShadoW

Active Member
Licensed User
Longtime User
As I wrote, professionals never use these two methods. They are never needed.
Please look at JSON.txt file what I have upload in previos post.
How can I Parse this as map?

B4X:
[B][COLOR=rgb(184, 49, 47)]Dim OPZJN7-RNVKA-GTVHN7 As Map[/COLOR][/B] = closed.Get("OPZJN7-RNVKA-GTVHN7")

Dim opentm As Double = OPZJN7-RNVKA-GTVHN7.Get("opentm")

Dim oflags As String = OPZJN7-RNVKA-GTVHN7.Get("oflags")

Dim reason As String = OPZJN7-RNVKA-GTVHN7.Get("reason")

Dim cost As String = OPZJN7-RNVKA-GTVHN7.Get("cost")
 
Upvote 0

G-ShadoW

Active Member
Licensed User
Longtime User
That's why I have ask, because NAME is XXXXX-XXXXX-XXXCC
I have search all over the forum and I have seen that someone else have same problem with parsing and without success!
 
Upvote 0
Top