Search code examples
iosarraysswiftparse-platformpfquery

Swift - how to return 'contained in' objects in same order as array?


I have an array that contains a list of objectId's in a very specific order. I'm then running a PFQuery, with a "containedIn" constraint. How do I order the results of this query so that the returned objects are in the same order as their corresponding objectId's in the array?

Thanks :)


Solution

  • PFQueries can only be ordered by the values in certain columns. So you could use the "orderByDesecending" or "orderByAscending" with a key (such as date, or alphabetized by name), but there is no "orderToMatchTheOrderOfMyArray."

    I would recommend just sorting them manually. It shouldn't be hard. I'm not a swift dev, but here is the pseudo code:

    sortedObjects = [] //start with empty array
    for each ObjectId in MyListOfObjectIds { //loop through your ordered objectIds
        for each object in MyQueriedObjects { //loop through the objects you got from the query
            if (ObjectId == object.objectId) { //if you've found the correct object
                sortedObjects.addObject(object); //add it to the list
                break; //move on to the next ordered objectId
            }
        }
    }