I'm using NSSortDescriptor
s to sort an Array
by the date element. The date is set as String
using a formatter which formats in the style : "dd/MM/yy HH:mm". This date element is stored inside dictionaries which are all stored in the array. Parts of my code for this are below:
// Date Formatting
let currentTime = Date()
let timeFormatter = DateFormatter()
timeFormatter.locale = Locale.current
timeFormatter.dateFormat = "HH:mm dd/MM/yy"
let convertedTime:String! = timeFormatter.string(from: currentTime)
// Descriptor
let descriptorD: NSSortDescriptor = NSSortDescriptor(key: "Date", ascending: false)
// Dictionary
let newUserRecord = [
"Name" : enteredName!,
"Score" : self.gameScore,
"Date" : convertedTime
] as [String : Any]
// Sorting
newUserArray.sort(using: [descriptorD])
However my problem is the date is only being sorted by the time (HH:mm) and not taking in account the (dd/MM/yy) part. For example if I sort by the date and have a date of 13:12 19/11/16 and a date of 09:12 18/11/16 the 09:12 date will appear first even though it should be the 13:12 as it is a day later. How do I fix this?