Fileview sort the elements by date

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
toto96
Posts: 8
Joined: Monday 24th April 2023 5:23pm

Fileview sort the elements by date

Post by toto96 »

Hello,
I would like to know how to sort the elements by date in a fileview, considering that I have a "showpreview" display.
Online
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Fileview sort the elements by date

Post by cogier »

I can't see a way to do this using FileView, but you might consider creating your own file display to do this. Have a look at attached, there is a lot more you could do with it, but I hope it's a start.
ShowPreview_Sort-0.0.1.tar.gz
(8.93 KiB) Downloaded 371 times
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fileview sort the elements by date

Post by BruceSteers »

It can be done with a little trickery.
You can hack the IconView inside the FileView

Remember this conversation...
https://forum.gambas.one/viewtopic.php?t=1532

You could set the IconView "Sorted" property to false and do it yourself.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fileview sort the elements by date

Post by BruceSteers »

Or a command something like this...


Public Sub SortFileViewByDate()

  Dim p As Panel = FileView1.Children[0]
  Dim iv As IconView = p.Children[0]
  
  Dim aKeys As New String[]
  Dim aDates As New Date[]

 ' make an array of the keys and an array of the modified dates...
  For Each sKey As String In iv.Keys
    aDates.Add(Stat(FileView1.Dir &/ iv[sKey].Text).LastModified)
    aKeys.Add(sKey)
  Next

' sort the list by date, swapping keys along with dates
'  (this sort function might need some work but it looks like it works to me)

ReCheck:
  For c As Integer = 0 To aDates.Max - 1
    If DateDiff(aDates[c], aDates[c + 1], gb.Second) > 0 Then
      Swap aDates[c], aDates[c + 1]
      Swap aKeys[c], aKeys[c + 1]
      Goto ReCheck
    Endif
  Next

  ' make the IconView match the list order by going through the list in reverse moving each item key to the top of the iconview.
  For c As Integer = aKeys.Max DownTo 0
    iv.MoveTo(aKeys[c])
    iv.Item.MoveFirst()
  Next

End

' now it is in date order

If at first you don't succeed , try doing something differently.
BruceS
toto96
Posts: 8
Joined: Monday 24th April 2023 5:23pm

Re: Fileview sort the elements by date

Post by toto96 »

Many thanks for your help! It works perfectly.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fileview sort the elements by date

Post by BruceSteers »

toto96 wrote: Friday 22nd March 2024 5:18pm Many thanks for your help! It works perfectly.
No worries.

btw if you want to reverse the order just reverse the 2 arrays before moving the items...


If bReverse Then ' reverse the order to show oldest first if required
  aKeys.Reverse
  aDates.Reverse
Endif

  ' make the IconView match the list order

  For c As Integer = aKeys.Max DownTo 0
    iv.MoveTo(aKeys[c])
    iv.Item.MoveFirst()
  Next
If at first you don't succeed , try doing something differently.
BruceS
Post Reply