swift - Array not getting sorted -
i have array
of structs
. in struct
have 2 nsdate
objects: textlabel
, detailtextlabel
. i'm trying sort textlabel
newest oldest date/time. , want detailtextlabel
ordered based on textlabel
. , want vice versa. here's code:
struct item { let textlabel : nsdate let detailtextlabel : nsdate } var myitem = [item]() myitem.insert(item(textlabel: mydatesecond, detailtextlabel: anotherdatesecond), atindex: 0) myitem.insert(item(textlabel: mydatethird, detailtextlabel: anotherdatethird), atindex: 0) myitem.insert(item(textlabel: mydatefirst, detailtextlabel: anotherdatefirst), atindex: 0) @ibaction func sortbuttonsaction(sender: uibutton) { if sender == firstbtn { myitem.sort({$0.textlabel.compare($1.detailtextlabel) == nscomparisonresult.orderedascending}) } else if sender == secondbtn { myitem.sort({$0.detailtextlabel.compare($1.textlabel) == nscomparisonresult.orderedascending}) } self.tableview.reloaddata() }
here's happens:
when select firstbtn
, gets sorted way i'm expecting to. when press secondbtn
, nothing happens.
what want happen
when secondbtn
gets selected, want array
sorted based on detailtextlabel
data. why isn't happening?
to sort textlabel date:
myitem.sort{$0.textlabel.compare($1.textlabel) == nscomparisonresult.orderedascending}
to sort detailtextlabel date:
myitem.sort{$0.detailtextlabel.compare($1.detailtextlabel) == nscomparisonresult.orderedascending}
you should choose better names vars. date, not label.
Comments
Post a Comment