Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAaron Watkins
10,902 PointsSort Array by date: Swift + Parse.com
Hi all!
I am looking to sort an array by date. I have combined an array from two different classes. The issue is that the objects from the second class are at the end of the array. I want all objects in the array to be sorted by the date on which they were created (i.e., createdAt date). I cannot figure out how to do this. When I print the combined array I all of the items in both of the individual arrays, but no "createdAt" key to sort on. Would anyone have any ideas on how to do this?
3 Answers
Aaron Watkins
10,902 Pointsthanks Jason.
I wound up doing this
combinedArray = array1 + array2 + array3
var descriptor: NSSortDescriptor = NSSortDescriptor(key:"createdAt", ascending: false)
sortedResults = combinedArray.sortedArrayUsingDescriptors([descriptor])
This worked, but problem I'm having now is that I can tell that the tableview objects are not in the right order when an object from a different array is inserted. For example,
cell 0: contains an object from array1. When I click on that cell, it takes me to a detail view with information for cell 0 cell 1: contains an object from array2. When I click on that cell, it takes me to a detail view with information for cell 2 (should be information from cell 1). ...
So the objects keep getting displaced every time an object from a new array is inserted into the list. Have you come across this?
thanks again for your help.
jason phillips
18,131 PointsCan you do something like:
array.sort({$0.createdAt > $1.createdAt })
Ryan Forte
iOS Development Techdegree Student 2,955 Pointscheck out this video, skip to the end and the line of code you are looking for is there. hope this helps. https://www.youtube.com/watch?v=SeL9NteSLjE&lc=z22aihfj1ybvs33vsacdp43bixck4drobt441esqxatw03c010c