Page 1 of 1

Fileview sort the elements by date

Posted: Friday 22nd March 2024 1:10pm
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.

Re: Fileview sort the elements by date

Posted: Friday 22nd March 2024 4:04pm
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 574 times

Re: Fileview sort the elements by date

Posted: Friday 22nd March 2024 4:05pm
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.

Re: Fileview sort the elements by date

Posted: Friday 22nd March 2024 4:35pm
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


Re: Fileview sort the elements by date

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

Re: Fileview sort the elements by date

Posted: Friday 22nd March 2024 5:34pm
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