dimanche 3 juillet 2016

Swift: search and randomly select in array (of Parse query results) but give precedence to newer objects?

I am basically trying to create a basic search algorithm in Swift for objects returned by my Parse query. Right now, when a user searches multiple object can match the search term. An array of the matching objects is returned and right now I can randomly select and display one with this:

let rand = Int(arc4random_uniform(UInt32(results!.count)))

        let selectedIdea = results![rand]
//then output selectedIdea

When I query, I can sort my query results by date using .orderByAscending("createdAt"). This all works well.

I need to know whether there is a way to still RANDOMLY select an object from this results array but give PRECEDENCE (so I guess not entirely randomly) to the objects at the front of the array? Meaning there is a highly likelihood of a newly posted object being selected than old?

If this isn't possible, how would one incrementally move through an array of objects (going from first index to last then cycling again) when I have different results arrays being returned each time (the user can search for a different term or the same term).

I don't want to just select the most recent object because this would mean this recent object would likely be selected and displayed multiple multiple times. Is there a way to start with most recent object and as that same results array is returned again, moving to next recent and next?




Aucun commentaire:

Enregistrer un commentaire