Stack108 Stack108 - 2 years ago 95
Swift Question

Swift reorder dynamic UITableView by NSDate

in my app with swift 2 programming language,i have a UITableView which will be fill with core date items.

As you can see in this post, i have already asked for a sorting function:

func ReSort() {
CoreDataItems.sortInPlace({sortAscending ? $0.RowName < $1.RowName : $0.RowName > $1.RowName})
sortAscending = !sortAscending

Now my question is, how can i set dynamically the RowName Value?
for example. i save the the RowName in the user defaults.

doesn't work without syntax error

Next question is:

How can i sort with a row, which value is a NSDate?

Thank you very much :)

Answer Source

You can sort NSArray which contains NSDate by using this code:

// objective-c code
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [nodeEventArray

//swift code
let dateDescriptor :NSSortDescriptor = NSSortDescriptor(key: "startDateTime", ascending: true)
let sortdescriptors: NSArray = [dateDescriptor]
let sortedeventarray = nodeEventArray.sortedArrayUsingDescriptors([dateDescriptor])

After sorting is done, you can reload your tableview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download