Problems with sort in a String[]

Post

Posted
Rating:
#1 (In Topic #922)
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 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?

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Is this what you want?

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim asArch As New String[]
  4.   Dim sWork As String
  5.  
  6.   asArch.Add("10/31/2022 16:35|2022-10-31_16.35_ContaDB.db")
  7.   asArch.Add("11/03/2022 17:23|2022-11-03_17.23_ContaDB.db")
  8.   asArch.Add("10/31/2022 12:05|2022-10-31_12.05_ContaDB.db")
  9.  
  10.   asArch.Sort(gb.Descent)
  11.  
  12.   For Each sWork In asArch
  13.     Print sWork
  14.   Next
  15.  
  16.   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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#4
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
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…

Code (gambas)

  1.  
  2. Public Sub Sort_Some_Date_Strings_Correctly()
  3.  
  4. Dim a As String[] = ["31/10/2022 16:35|2022-10-31_16.35_ContaDB.db",
  5.  "03/11/2022 17:23|2022-11-03_17.23_ContaDB.db",
  6.  "31/10/2022 12:05|2022-10-31_12.05_ContaDB.db"]
  7.  
  8. a = DateSort(a)
  9.  
  10. Print a.Join("\n")
  11.  
  12.  
  13.  
  14. Public Sub DateSort(Data As String[]) As String[]
  15.  
  16.   Dim sStr As String, aDate As String[], sDate As String
  17.   Dim aStr As String[] = Data.Copy()
  18.   Dim bIsReversed As Boolean
  19.   For c As Integer = 0 To aStr.Max
  20.    sStr = aStr[c]
  21.    sDate = Split(sStr, " ")[0]
  22.    aDate = Split(sDate, "/")
  23.     If aDate.Last.Len = 4 Then
  24.      aStr[c] = Replace(sStr, sDate, aDate.Reverse().Join("/"))
  25.     Endif
  26.   Next
  27.  
  28.   aStr = aStr.Sort(gb.Descent)
  29.   For c As Integer = 0 To aStr.Max
  30.    sStr = aStr[c]
  31.    sDate = Split(sStr, " ")[0]
  32.    aDate = Split(sDate, "/")
  33.    aStr[c] = Replace(sStr, sDate, aDate.Reverse().Join("/"))
  34.   Next
  35.  
  36.   Return aStr
  37.  
  38.  
  39.  

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
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
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

Code (gambas)

  1. Public Sub DateSort(Data As String[]) As String[]
  2.  
  3.   Dim aStr As String[] = Data.Copy()
  4.  
  5.   For c As Integer = 0 To aStr.Max
  6.    aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  7.    Next
  8.  
  9.   aStr = aStr.Sort(gb.Descent)
  10.  
  11.   For c As Integer = 0 To aStr.Max
  12.    aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  13.   Next
  14.  
  15.   Return aStr
  16.  
  17.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
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. :)
Online now: No Back to the top

Post

Posted
Rating:
#8
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 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. :)
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

gambafeliz said

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 ;)
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’

gambafeliz said

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 |

Code (gambas)

  1.  
  2.    aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  3.  
  4.  

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…

Code (gambas)

  1.  
  2. Public Sub DateSort(Data As String[]) As String[]
  3.    
  4.   Dim aStr As String[] = Data.Copy()  ' Make a copy of the array
  5.  
  6. ' Spin each item at the | so it is filename|date
  7.   For c As Integer = 0 To aStr.Max
  8.    aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  9.    Next
  10.  
  11. ' Sort it
  12.   aStr = aStr.Sort(gb.Descent)
  13.  
  14. ' Spin the data back to date|filename
  15.   For c As Integer = 0 To aStr.Max
  16.    aStr[c] = Split(aStr[c], "|").Reverse().Join("|")
  17.   Next
  18.  
  19.   Return aStr  ' result
  20.  
  21.  
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 You are definitely from that place.

Thank you both for your help. You are amazing.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
No worries :)

PS. something else to consider.
A String returned by various Date() string functions may differ in format depending on location so it is best to never use it for things like sorting or other purposes that may assume it's format if your program may be used in another country.

the filenames there though,, excelente :)
Online now: No Back to the top

Post

Posted
Rating:
#13
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
Following your recommendation or so I think :)

My input will do this:
2022-11-03_17.23_ContaDB.db
2022-10-31_13.23_CountDB.db
2022-11-02_12.23_CountDB.db

This has been like this:

1. I generate a String[] with the names of the .db files
2. Next I use Sort(gb.Descent) to sort the dates from Highest to Lowest.
3. Only in element 0 of the String[] I extract the date and time and convert it to Spanish date and time.
4. I compare this date with -3 hours from Now to make the copy or not.
5. Finally I move the .db file of the last element of the String[] to a historical folder.

And ready, I hope this seems correct to you.

At the moment it works for me. What do you think?

Thank you for your effort.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#14
Guru
BruceSteers is in the usergroup ‘Guru’

gambafeliz said

Following your recommendation or so I think :)

My input will do this:
2022-11-03_17.23_ContaDB.db
2022-10-31_13.23_CountDB.db
2022-11-02_12.23_CountDB.db

This has been like this:

1. I generate a String[] with the names of the .db files
2. Next I use Sort(gb.Descent) to sort the dates from Highest to Lowest.
3. Only in element 0 of the String[] I extract the date and time and convert it to Spanish date and time.
4. I compare this date with -3 hours from Now to make the copy or not.
5. Finally I move the .db file of the last element of the String[] to a historical folder.

And ready, I hope this seems correct to you.

At the moment it works for me. What do you think?

Thank you for your effort.

I'm sure it works :)
i did a little date function to get a Date object from the filename (or the whole string) in my test app..
it returns Date object from the string that should display correctly in any locale (did not consider timezone offsets)
probably yours does a similar thing.

Code (gambas)

  1. Public Sub DateFromString(db_string As String) As Date
  2.  
  3.   Dim s As String = db_string
  4.   If InStr(s, "|") Then s = Split(s, "|")[Split(s, "|").Find("*DB.db", gb.Like)] ' get the filename from a | split string
  5.  
  6.   Dim a As String[] = Split(s, "_")
  7.   Dim aDate As String[] = Split(a[0], "-")
  8.   Dim aTime As String[] = Split(a[1], ".")
  9.  
  10.   Dim d As Date
  11.   d = Date(aDate[0], aDate[1], aDate[2], aTime[0], aTime[1])
  12.   Return d
  13.  
  14.  
  15.  

I thought with something like that you could forget about having to store the date string and just use that function if you want to use the locally formatted date string in anything.

Glad you figured it out :)
Online now: No Back to the top

Post

Posted
Rating:
#15
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 Thanks BruceS

As I do not handle the forum very well. I don't know many things like if I can close this post as solved and I don't even know how to refer to something you write to be able to talk about it. I hope to be able to do it soon.

Sincerely, truly believe that I greatly appreciate your efforts and help. Thanks.

I'll keep in touch. Wishing you have a happy weekend.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#16
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 Just commenting on your last function is interesting but I don't know why if I pass it 2022-11-04_17.03_ContaDB.db it returns me in a message.info (your function) this: 11/04/2022 16:03:00 instead of 11/04/2022 17:03:00 and for me it would be better 11/04/2022 17:03 nothing else.

There I leave it to you. For you to think about it. And sorry for how annoying I am.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.