John Doe
John Doe

Reputation: 1609

Organized Combined Array From Parse By Date

I combined PFObject arrays together that I got from Parse...

array.appendContentsOf(otherArray)

How would I take this one big array and organize it by when they were created?

Here is basically what my query is, I took out most stuff and changed some stuff around so it would make sense if you didn't see my entire class...

func loadStuff() {

    let postQuery = PFQuery(className: "Post")

    postQuery.orderByDescending("createdAt")

    postQuery.whereKey("type", equalTo: "world")

    postQuery.whereKey("distance", equalTo: distanceType)

    postQuery.whereKey("location", nearGeoPoint: PFGeoPoint(location: currentLocation), withinMiles: miles)

    postQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
        if let objects = objects as? [PFObject] {

          // Remove content from arrays if it's the first query

            if error == nil {
                if arrayOfPosts.isEmpty == false {
                    for post in arrayOfPosts {

                     // Append stuff

                        if queryIsComplete == true {

                            if query == 0 {
                                self.loadStuff()

                            } else if query == 1 {

                                self.loadStuff()

                            } else {

                                self.arrays.appendContentsOf(self.arrayFromQuery1)
                                self.arrays.appendContentsOf(self.arrayFromQuery2)
                                self.arrays.appendContentsOf(self.arrayFromQuery3)

                                self.arrays.sortInPlace({ $0.createdAt!.compare($1.createdAt!) == NSComparisonResult.OrderedAscending })

                                self.table.reloadData()

                            }


                        }


                    }

                }

            }
        }
    })
}

Upvotes: 1

Views: 56

Answers (2)

Lamour
Lamour

Reputation: 3030

if you want to know when each object was created inside of your array,you will put a condition before you start the query from parse using the timestamp createAt or updateAt

 var timeStamp = NSDate(timeIntervalSinceNow: -1200)
 query.whereKey("createdAt", greaterThanOrEqualTo: timeStamp)

Then you could order your list just like the following

   query.orderByDescending("createdAt")   //either Ascending or Descending

So now you will do what my friend @DavidWong propose into the top post :)

Upvotes: 0

David Wong
David Wong

Reputation: 10294

I'm assuming you're fetching a whole lot of records asynchronously so:

array.sortInPlace { $0.createdAt.compare($1.createdAt) == NSComparisonResult.OrderedAscending }

Upvotes: 2

Related Questions