Android Question SortType by DateTime field

luke2012

Well-Known Member
Licensed User
Longtime User
Hi all,
I'm using a B4XOrderedMap object (by @Erel) and a list in order to implement a sort using a date field that I got from a remote server (REST API).
The field has this data and format:

- Format: Y-m-d\TH:i:sP
- Data (data field content): 2020-04-26T12:36:07+00:00

See the relevant code:
B4X:
'Class Globals
Public mapArtIMGs As B4XOrderedMap

Sub GetArtContent (nid As Int, aUser As String, aPsw As String) As ResumableSub
       'Here I read the date time field from the JSON data
       Dim CFullDate As String = colchanged.Get("value")    '--> this contains "2020-04-26T12:36:07+00:00"
       Dim HDate As String = CFullDate.SubString2 (0, 10)   '--> In this way I got only the date substring used within the sort
End sub

Public Sub LoadArtList (ArtContent As B4XOrderedMap)
    Private lstArt As List = ArtContent.Values
    lstArt.SortType("HDate", False)  ---> Apply sort on HDate ("2020-04-26")
    For i=0 To lstArt.Size-1
    '....
     mapArtIMGs.Put(target_id, Item)  
    '....
    Next
    Return mapArtIMGs
End Sub

In this way I sort only by date, but how to obtain a full timestamp using the downloaded date time: 2020-04-26T12:36:07+00:00 ?
I need to sort the list by timestamp or al least by YYYY-MM-DD-HH-MM.
What is the simple and quick way to obtain a full time stamp or a partial timestamp (YYYY-MM-DD-HH-MM) using "2020-04-26T12:36:07+00:00" (Format: Y-m-d\TH:i:sP) ?

Thanks in advance for your help :)
Luca.
 

DonManfred

Expert
Licensed User
Longtime User
how to obtain a full timestamp using the downloaded date time: 2020-04-26T12:36:07+00:00 ?
Sort will work with this String i guess. Is there a problem with sorting a list with this Strings?

You can also create a CustomType and use the String in one of the Values/Fields. You can sort this list with
B4X:
list.SortType("MyCustomTypefield")
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Sort will work with this String i guess. Is there a problem with sorting a list with this Strings?

You can also create a CustomType and use the String in one of the Values/Fields. You can sort this list with
B4X:
list.SortType("MyCustomTypefield")

You mean that I can directly sort this type of strings: "2020-04-26T12:36:07+00:00" and it works as it is as time stamp ?

B4X:
Private lstArt As List = ArtContent.Values
lstArt.SortType("HDate", False)

I mean, my target is to sort a list of news articles by date and time starting from the most recent to the old one.

Within the (drupal) site the articles are displayed (correctly) in this exact order:

Article A - 14/08/2020 - 18:50
Article B - 13/08/2020 - 18:13
Article C - 13/08/2020 - 17:46
Article D - 13/08/2020 - 17:26
Article E - 13/08/2020 - 11:31
Article F - 29/06/2020 - 11:54
Article G - 26/04/2020 - 14:00

I have to replicate the same sort order within the app.
 
Last edited:
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
This might be over-thinking it but why not just convert the UTC date to a long integer and sort that? I coded something like that some time ago to deal with a UTC date from a SQL query I was receiving:
Convert UTC string to local tick value.:
'  Takes a SQL UTC date string and returns the ticks for that time
Public Sub UtcStringToLocalDate(UTC As String) As Long
    Dim psCurrFmt As String
    Dim piCurrOff As Int
    Dim plTicks As Long
    Dim psDate As String
    Dim psFmt As String

    If UTC.Trim = "" Then
        Return 0
    End If

    psCurrFmt = DateTime.DateFormat
    piCurrOff = DateTime.TimeZoneOffset
    psDate = UTC.Trim.ToUpperCase
    Dim piPos As Int = psDate.IndexOf(".")
    If piPos > 0 Then
        Dim piLen As Int = psDate.length - piPos
        Dim piIndex As Int
        psFmt = "yyyy-MM-dd'T'HH:mm:ss.S"
        For piIndex = 1 To piLen - 2
            psFmt = psFmt & "S"
        Next
        psFmt = psFmt & "Z"
    Else
        psFmt = "yyyy-MM-dd'T'HH:mm:ssZ"
    End If
    DateTime.DateFormat = psFmt
    If psDate.Length > 5 Then
        Dim psCheck As String = psDate.SubString2(psDate.Length - 5, psDate.Length)
        If Not(psCheck.StartsWith("+")) And Not(psCheck.StartsWith("-")) Then
            psDate = psDate & "+0000"
        End If
    End If
  
    DateTime.SetTimeZone(0)
    Try
        plTicks = DateTime.DateParse(psDate)
    Catch
        plTicks = 0
    End Try

    DateTime.DateFormat = psCurrFmt
    DateTime.SetTimeZone(piCurrOff)
    
    Return plTicks
End Sub
 
Upvote 0
Top