ios - How to sort array based on another arrays position? -
i have tableview
style being right detail
. therefore have 2 arrays
, 1 textlabels
data, , other detailtextlabel
.
there 2 "sort by" options. 1 sort textlabels
data, , second sort detailtextlabels
data. when sort first array
(textlabels array
), second array (detailtextlables
array) have sorted based on first
array`.
i know how sort arrays
, how can sort 1 array
based on another?
here's how sorted array
: (it's array
of dates
.
firstarray.sort({ (a, b) -> bool in a.earlierdate(b) == })
it's bit messy, can use enumerate
work indices , elements @ same time:
array1.enumerate().sort { return $0.element < $1.element }.map {$0.element} array1.enumerate().sort { return array2[$0.index] < array2[$1.index] }.map {$0.element}
but it's simpler/easier 1 array.
struct item { let prop1: int let prop2: string } var array = [ item(prop1: 1, prop2: "c"), item(prop1: 2, prop2: "b"), item(prop1: 3, prop2: "a") ] array.sort { $0.prop1 < $1.prop1 } array.sort { $0.prop2 < $1.prop2 }
Comments
Post a Comment