I have an array of SortedELRStructure that I need to sort by it's elr property, AS WELL AS by it's railwayID. The trouble I am having is the railwayID is populated using a string value, as it could be anything like ["1A", "C0284", "300000"] etc so when I sort the array by railwayID is sorts them as strings and I get back ["101023", "10A", "11", "12", "110032"] (example).
How do I sort an array of this type by the numerical value of railwayID, even when it contains letters, in addition to sorting by the elr string?
I've tried the following so far, which I think getting close but the railway ID is still being sorted as a string:
let sorted = sortedDayListItems.sorted { t1, t2 in
if t1.elr == t2.elr {
if t1.railwayID.isInt && t2.railwayID.isInt {
return Int(t1.railwayID)! < Int(t2.railwayID)!
} else {
}
return t1.railwayID < t2.railwayID
}
return (t1.elr != nil) && (t2.elr == nil)
}
struct SortedELRStructure {
var dataItem: CalendarSurveyDataItem
var elr: String
var railwayID: String
}
ralwayIDwhen theirelrare the same, and it's exactly the linked question (with multiple answers to adapt maybe to your case, because it's unclear in where "C0284" would go against "B01", before or after it?)["10A", "11", "12", "101023", "110032"]?