Problems with sort in a String[]

New to Gambas? Post your questions here. No question is too silly or too simple.
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Problems with sort in a String[]

Post by gambafeliz »

Hi

I want to sort a String[] from Highest to Lowest where:
0 - 100
1 - 90
2 - 80

But it gives me different results since I changed the version of the operating system and with it Gambas.

This is the String[], real example:

Dim asArchivosDBCorregido As New String[]

(messy, just like i charge it) asArchivosDBCorregido
0 "31/10/2022 16:35|2022-10-31_16.35_ContaDB.db"
1 "03/11/2022 17:23|2022-11-03_17.23_ContaDB.db"
2 "31/10/2022 12:05|2022-10-31_12.05_ContaDB.db"

(after using Sort) = asArchivosDBCorregido.Sort(gb.Descent)
0 "31/10/2022 16:35|2022-10-31_16.35_ContaDB.db"
1 "31/10/2022 12:05|2022-10-31_12.05_ContaDB.db"
2 "03/11/2022 17:23|2022-11-03_17.23_ContaDB.db"

What can be past? any suggestion?

Note: I have tried Ascendant and descendant but in both situations something fails at some point, although I have not determined it.

Should I solve it myself without using Sort?
Last edited by gambafeliz on Thursday 3rd November 2022 5:16pm, edited 1 time in total.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Problems with sort in a String[]

Post by cogier »

Is this what you want?
Public Sub Form_Open()

  Dim asArch As New String[]
  Dim sWork As String

  asArch.Add("10/31/2022 16:35|2022-10-31_16.35_ContaDB.db")
  asArch.Add("11/03/2022 17:23|2022-11-03_17.23_ContaDB.db")
  asArch.Add("10/31/2022 12:05|2022-10-31_12.05_ContaDB.db")

  asArch.Sort(gb.Descent)

  For Each sWork In asArch
    Print sWork
  Next

  End
Output: -

11/03/2022 17:23|2022-11-03_17.23_ContaDB.db
10/31/2022 16:35|2022-10-31_16.35_ContaDB.db
10/31/2022 12:05|2022-10-31_12.05_ContaDB.db

If not, please post some example code which shows the problem.
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Problems with sort in a String[]

Post by gambafeliz »

Thank you but I changed the data due to post editing (please note). In addition, what happens happens to me whether I try ascending or descending. It seems that these data cannot be processed by Sort because they are incompatible.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Problems with sort in a String[]

Post by gambafeliz »

I think I have detected the problem. I intend Sort to sort dates but sort is just sorting strings, or so I think. How can I order my case, someone guide me. Thanks.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

Trouble is probably your date string format has changed, the sort is correct. you need to spin the dates so they are Year/Month/Day to correctly sort.

you could spin the dates , sort the array, then spin them back...

Public Sub Sort_Some_Date_Strings_Correctly()
 
Dim a As String[] = ["31/10/2022 16:35|2022-10-31_16.35_ContaDB.db",
 "03/11/2022 17:23|2022-11-03_17.23_ContaDB.db",
 "31/10/2022 12:05|2022-10-31_12.05_ContaDB.db"]

a = DateSort(a)

Print a.Join("\n")

End


Public Sub DateSort(Data As String[]) As String[]
  
  Dim sStr As String, aDate As String[], sDate As String
  Dim aStr As String[] = Data.Copy()
  Dim bIsReversed As Boolean
  For c As Integer = 0 To aStr.Max
   sStr = aStr[c]
   sDate = Split(sStr, " ")[0]
   aDate = Split(sDate, "/")
    If aDate.Last.Len = 4 Then
     aStr[c] = Replace(sStr, sDate, aDate.Reverse().Join("/"))
    Endif
  Next

  aStr = aStr.Sort(gb.Descent)
  For c As Integer = 0 To aStr.Max
   sStr = aStr[c]
   sDate = Split(sStr, " ")[0]
   aDate = Split(sDate, "/")
   aStr[c] = Replace(sStr, sDate, aDate.Reverse().Join("/"))
  Next

  Return aStr

End

Output:
03/11/2022 17:23|2022-11-03_17.23_ContaDB.db
31/10/2022 16:35|2022-10-31_16.35_ContaDB.db
31/10/2022 12:05|2022-10-31_12.05_ContaDB.db
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

or if each string format was the other way round..

"2022-10-31_12.05_ContaDB.db|31/10/2022 12:05"

or is the date string even needed? all the information is in the filenames
the name 2022-10-31_12.05_ContaDB.db has the date correctly formatted for sorting and the time, the other string seems unneeded.

thinking about it this would be quicker to just reverse at the | mark then sort and re-reverse
Public Sub DateSort(Data As String[]) As String[]
  
  Dim aStr As String[] = Data.Copy()

  For c As Integer = 0 To aStr.Max
   aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
   Next

  aStr = aStr.Sort(gb.Descent)

  For c As Integer = 0 To aStr.Max
   aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  Next

  Return aStr

End
If at first you don't succeed , try doing something differently.
BruceS
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Problems with sort in a String[]

Post by gambafeliz »

This makes a lot of sense and I'm almost sure it's the solution, I'm going to start it because honestly my case was driving me a little crazy.

But hey, where are you from, you won't be the first Martian I know. Hey, it seems to me that according to your photo you don't look like it. :D

Thank you
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
gambafeliz
Posts: 139
Joined: Friday 2nd September 2022 7:50pm
Location: I live in the same city as Picasso

Re: Problems with sort in a String[]

Post by gambafeliz »

Sorry for my ignorance but in the second example code is it with date|file.db or do you only deal with file.db

I am thinking that you are right in the deduction of only file.db since, as you say, it is well treated. I then get date and time to inform the user of the same file name.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

gambafeliz wrote: Thursday 3rd November 2022 5:36pm This makes a lot of sense and I'm almost sure it's the solution, I'm going to start it because honestly my case was driving me a little crazy.

But hey, where are you from, you won't be the first Martian I know. Hey, it seems to me that according to your photo you don't look like it. :D

Thank you
you are welcome.
yes it is for sure what's wrong.

using year / month / day (as in the filenames) and not day / month / year for the sort will fix it

I am from the matrix , welcome to the real world Neo ;)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

gambafeliz wrote: Thursday 3rd November 2022 5:44pm Sorry for my ignorance but in the second example code is it with date|file.db or do you only deal with file.db

I am thinking that you are right in the deduction of only file.db since, as you say, it is well treated. I then get date and time to inform the user of the same file name.
the second example flips the 2 strings separated by the |
  
   aStr[c] = Split(aStr[c], "|").Reverse().Join("|")

This makes
03/11/2022 17:23|2022-11-03_17.23_ContaDB.db
become this ..
2022-11-03_17.23_ContaDB.db|03/11/2022 17:23


then is sorts the new array , then returns with the strings flipped back to 03/11/2022 17:23|2022-11-03_17.23_ContaDB.db format.

here it is with comments...

Public Sub DateSort(Data As String[]) As String[]
   
  Dim aStr As String[] = Data.Copy()  ' Make a copy of the array
 
' Spin each item at the | so it is filename|date
  For c As Integer = 0 To aStr.Max
   aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
   Next

' Sort it 
  aStr = aStr.Sort(gb.Descent)
 
' Spin the data back to date|filename
  For c As Integer = 0 To aStr.Max
   aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  Next
 
  Return aStr  ' result
 
End
If at first you don't succeed , try doing something differently.
BruceS
Post Reply