0

When fetching assets from the user's photo library, you must specify how you want the photos to be sorted. You do this by passing in a NSSortDescriptor(). There are a few completions, but I'm wondering if there's any way to pass in a "random sort".

I'm hoping there's something similar to the .sort function used like this:

var arr = [1, 2, 3]
arr = arr.sorted { Bool.random() }   // Returns the array, sorted randomly

Is there any way I can achieve a "random sort" with NSSortDescriptor() ?

If not, is there any way to fetch random photos from a user's library (without fetching all of them and then sorting on those randomly) ?

Stoic
  • 945
  • 12
  • 22

1 Answers1

1

I think you need to use a comporator in NSSortDescriptor for flexible sorting:

How to create an NSSortDescriptor using the comparator signature?

neskafesha
  • 191
  • 5
  • This helps, thanks. I can't seem to find a way to use it to sort them randomly, though. Do you know a way? – Stoic Sep 30 '22 at 22:11
  • 1
    I have just checked the operation of the comparator in fetched a photo in photoKit. Comparator not working. I think it doesn't work for photoKit as for Core Data - > https://stackoverflow.com/questions/14938653/unsupported-nssortdescriptor-comparator-blocks-are-not-supported – neskafesha Sep 30 '22 at 23:52