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

Re: Problems with sort in a String[]

Post by gambafeliz »

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. :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

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 :)
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 »

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. :)
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Problems with sort in a String[]

Post by BruceSteers »

gambafeliz wrote: Friday 4th November 2022 11:57am 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.
Public Sub DateFromString(db_string As String) As Date
  
  Dim s As String = db_string
  If InStr(s, "|") Then s = Split(s, "|")[Split(s, "|").Find("*DB.db", gb.Like)] ' get the filename from a | split string

  Dim a As String[] = Split(s, "_")
  Dim aDate As String[] = Split(a[0], "-")
  Dim aTime As String[] = Split(a[1], ".")

  Dim d As Date
  d = Date(aDate[0], aDate[1], aDate[2], aTime[0], aTime[1])
  Return d
  
End

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 :)
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 »

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. :)
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 »

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. :)
Post Reply