あなたは[barStruct(今夜: "オープン"、距離:9)2つのソート条件
struct barStruct{
var tonight : String
var distance : Int
}
var bars = [barStruct(tonight: "Closed", distance: 12),
barStruct(tonight: "Closed", distance: 20),
barStruct(tonight: "Closed", distance: 1),
barStruct(tonight: "Closed", distance: 32),
barStruct(tonight: "Open", distance: 11),
barStruct(tonight: "Open", distance: 9),
barStruct(tonight: "Open", distance: 23),
barStruct(tonight: "Open", distance: 56),]
bars.sort { (lhs: barStruct, rhs: barStruct) -> Bool in
if lhs.tonight == "Closed"{
if rhs.tonight == "Closed"{
//both on bottom level, addtionaly sort by distance
return lhs.distance < rhs.distance
}else{
//left on bottom level, right on top level
return false
}
}else{
if rhs.tonight == "Closed"{
//left on top level, right on bottom level
return true
}else{
//both on top level, addtionaly sort by distance
return lhs.distance < rhs.distance
}
}
}
print(bars)
に.tonight
と.distance
を分割する必要があり、barStruct(今夜: "オープン"、 barStruct(tonight: "open"、distance:23)、barStruct(tonight: "Open"、distance:56)、barStruct(tonight: "Closed"、distance:1)、barStruct "、距離:12)、barStruct(今夜:"閉鎖 "、距離:20)、barStruct(今夜:"閉鎖 "、距離:32)]
それはあなたが投稿する必要があります –
大文字で始まるあなたの構造体に名前を付けるスウィフト慣例がありますあなたの構造宣言とあなたの望む結果 –