Problems with sort in a String[]
Posted
#1
(In Topic #922)
Enthusiast

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.
Posted
Guru

Code (gambas)
- 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)
- Print sWork
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.
Posted
Enthusiast

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

you could spin the dates , sort the array, then spin them back…
Code (gambas)
- "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)
- sStr = aStr[c]
- aStr = aStr.Sort(gb.Descent)
- sStr = aStr[c]
- Return aStr
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
Posted
Guru

"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)
- aStr = aStr.Sort(gb.Descent)
- Return aStr
Posted
Enthusiast

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.
Thank you
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

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.
Posted
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.![]()
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
Posted
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)
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)
- ' Spin each item at the | so it is filename|date
- ' Sort it
- aStr = aStr.Sort(gb.Descent)
- ' Spin the data back to date|filename
- Return aStr ' result
Posted
Enthusiast

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.
Posted
Guru

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
Posted
Enthusiast

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.
Posted
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)
- Return d
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
Posted
Enthusiast

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.
Posted
Enthusiast

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.
1 guest and 0 members have just viewed this.



