PhotoKit: How To Sort Photos Randomly

112 views Asked by At

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) ?

1

There are 1 answers

2
neskafesha On

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

How to create an NSSortDescriptor using the comparator signature?