Sorting a Picture array

I was working on a slideshow project which needs to show multiple pics in certain order. I want to sort the picture array by names. But I found out that picture array cannot be sorted, either the folderitem. What’s the best way to do that?

depends on how you have decided to store you pictures…
please post some code showing what you have attempted

You may be interested with Array.SortWith when it comes to sorting.

[quote=422158:@Dave S]depends on how you have decided to store you pictures…
please post some code showing what you have attempted[/quote]

Not much code I can put here, but I did take the Slideshow example in xojo webapp as reference.
The Pics in my program is stated as “Pics(-1) As Picture”.

[code]// Load the slideshow pictures into an array
Dim picFolder As FolderItem
picFolder = GetPicsFolder

If picFolder <> Nil Then
Dim f As FolderItem
Dim p As Picture

For i As Integer = 1 to picFolder.Count
f = picFolder.Item(i)
If f.Visible Then
p = Picture.Open(f)
If p <> Nil Then // it’s a picture and not some other kind of file
Pics.Append§
End If
End If
Next
End If[/code]

The GetPicsFolder method is following code

[code]Dim f As FolderItem
f = App.ExecutableFile.Parent.Child(“Pics”)

If f.Exists Then
Return f
Else
Return Nil
End If[/code]

ideally, I will have a “Pics” folder at the same folder with my exe file and in the “Pics” folder I plan to rename every pictures by sequence number, like 001.jpg, 002.jpg,…, etc. so the slideshow program will load all those pictures in a picture array and just play it thru a timer.

PS: I do find my program seems running as I expected under windows OS, but when I try this on my Raspberry Pi board, it won’t work. The pictures sequence looks like randomly decided.

nope, I have already tried that, the picture array doesn’t have the sort/sortwith method.

assuming PICS is what you want sorted… and also assume (since you didn’t specify) that it is PIC() as picture
which of course CANNOT be sorted…

try DIM p() as PAIR

then

dim myPair as Pair
myPair = p:f.displayname
p.append(myPair)

then create a method to sort by p().right (which would be the filename)

OR

create TWO array one for picture one for filename
then use SORTWITH

You need two arrays, one with file names, one with the folder items. Sort the folder items with the file names.

// Load the slideshow pictures into an array
DIM NAMES(-1) AS STRING
Dim picFolder As FolderItem
picFolder = GetPicsFolder

If picFolder <> Nil Then
Dim f As FolderItem
Dim p As Picture

For i As Integer = 1 to picFolder.Count
f = picFolder.Item(i)
If f.Visible Then
   p = Picture.Open(f)
   If p <> Nil Then  // it's a picture and not some other kind of file
      Pics.Append(p)
      NAMES.APPEND(F.DISPLAYNAME)
   End If
End If
Next
End If

//
PICS.SORTWITH(NAMES)

[quote=422164:@Dave S][code]
// Load the slideshow pictures into an array
DIM NAMES(-1) AS STRING
Dim picFolder As FolderItem
picFolder = GetPicsFolder

If picFolder <> Nil Then
Dim f As FolderItem
Dim p As Picture

For i As Integer = 1 to picFolder.Count
f = picFolder.Item(i)
If f.Visible Then
p = Picture.Open(f)
If p <> Nil Then // it’s a picture and not some other kind of file
Pics.Append§
NAMES.APPEND(F.DISPLAYNAME)
End If
End If
Next
End If

//
PICS.SORTWITH(NAMES)
[/code][/quote]

Pics in my program is a picture array. So there is no sort method or sortwith method. I noticed you using “PICS” all uppercase letter, is that a pair, which is something like PICS = f : f.displayName ?

I put them in CAPTIAL to make it easier to see what I changed.

Did you TRY it… an array is an array… regardless of datatype…

[quote=422173:@Dave S]I put them in CAPTIAL to make it easier to see what I changed.

Did you TRY it… an array is an array… regardless of datatype…[/quote]
I did try it. It won’t work. the error msg is
“App.Open, line 33
This kind of array can not be sorted
Pics.sortwith(fileNames)”

The Pics in my program is stated as “Pics(-1) As Picture”. I think that’s the reason, but I don’t know what type should I change to.

Y’all both need to check the docs for SortWith :wink:

Here is the screenshot. once the Pics() array as Picture, there is no method called “sort” or “sortwith”
The supported methods are “Append, Insert, pop, remove, shuffle”

https://1drv.ms/u/s!AnYGcCx4dZbNgsIAexx-YoPmrKl_5w

Yes, I did check that.
“The base array used with the Sortwith method works with Integer (and related types), String, Text, Single, and Double arrays only.”
It won’t work. same thing with “Sort” method.

Try sorting the names, not the pics like:

filenames.sortwith Pics

[quote=422185:@Jason Parsley]Try sorting the names, not the pics like:

filenames.sortwith Pics [/quote]
It doesn’t make any sense to me.

If that’s the case and you would like someone to implement your app, you can hire a Xojo consultant: https://www.xojo.com/resources/consultants.php

Maybe this topic in the docs might help:

https://documentation.xojo.com/getting_started/object-oriented_programming/advanced_oop_features.html#Sorting_Arrays_of_Classes

@Paul Lefebvre
Thank you very very much to provide the docs link. That helps a lot.

To anyone who read this post, the final result is good. I choose to use the sortwith method to sort the pictures name by the picture. It’s not intuitive for me. But as Paul’s link and thanks to the video too, it works.

[code]Dim fileNames(-1) As Text

// Load the slideshow pictures into an array
Dim picFolder As FolderItem
picFolder = GetPicsFolder

If picFolder <> Nil Then
Dim f As FolderItem
Dim pic As Picture

For i As Integer = 1 to picFolder.Count
f = picFolder.Item(i)
If f.Visible Then
pic = Picture.Open(f)
If pic <> Nil Then // it’s a picture and not some other kind of file
Pics.Append(pic)
fileNames.Append(f.Name.ToText)
End If
End If
Next i
End If

fileNames.SortWith(Pics)[/code]