我想首先对我的阵列进行排序,无论建立是关闭还是距离。例如,所有“已关闭”的企业应位于动态表的底部,然后应对顶部距离最小的其余企业进行排序。现在,我有以下代码:
bars.sort{ (lhs: barStruct, rhs: barStruct) -> Bool in
if lhs.tonight == "Closed" && rhs.tonight != "Closed"{
return false
}
else {
return lhs.distance < rhs.distance
}
现在,这只是在某些时候这样做
答案 0 :(得分:1)
你需要考虑Closed和amp;的所有组合。打开,不只是lhs
已关闭且rhs
已打开。我认为您所遵循的顺序是:
bars.sort { (lhs : barStruct, rhs : barStruct) -> Bool in
if lhs.tonight == "Closed"
{
return false // Closed/Open & Closed/Closed
}
else if rhs.tonight == "Closed"
{
return true; // Open/Closed
}
else
{
return lhs.distance < rhs.distance // Open/Open => use distance
}
}
然而,这并没有以任何方式对封闭条进行排序,也可能更好地按距离排序:
bars.sort { (lhs : barStruct, rhs : barStruct) -> Bool in
if lhs.tonight == "Closed"
{
if rhs.tonight == "Closed"
{
return lhs.distance < rhs.distance // Closed/Closed => use distance
}
else
{
return false // Closed/Open
}
}
else if rhs.tonight == "Closed"
{
return true; // Open/Closed
}
else
{
return lhs.distance < rhs.distance // Open/Open => use distance
}
}
现在,如果你有一个布尔标志,而不是一个字符串,那么对于封闭/打开你可以将它减少到单个,如果有一个xor条件......这是一个练习!
HTH
答案 1 :(得分:0)
您应该将.tonight
和.distance
划分为两种排序条件
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)
[barStruct(今晚:“开放”,距离:9),barStruct(今晚:“开放”,距离:11),barStruct(今晚:“开放”,距离:23),barStruct(今晚:“开放” ,距离:56),barStruct(今晚:“关闭”,距离:1),barStruct(今晚:“关闭”,距离:12),barStruct(今晚:“关闭”,距离:20),barStruct(今晚:“封闭“,距离:32)]
答案 2 :(得分:0)
您必须将条件设置为最小,并使用两个键tonight
和distance
。
1)按升序排序tonight
和..
2)放置条件或distance
升序
前:
struct testStruct {
var tonight : String
var distance : Int
}
var list = [testStruct(tonight: "Closed", distance: 1),
testStruct(tonight: "Closed", distance: 78),
testStruct(tonight: "Closed", distance: 14),
testStruct(tonight: "Closed", distance: 36),
testStruct(tonight: "Open", distance: 34),
testStruct(tonight: "Open", distance: 94),
testStruct(tonight: "Closed", distance: 3),
testStruct(tonight: "Open", distance: 56),]
// sort condition
let sortedList = list.sorted { (lhs, rhs ) -> Bool in
if lhs.tonight == rhs.tonight { // If tonight is same then step 2
return lhs.distance < rhs.distance
}
// Follow step 1
return (lhs.tonight) > (rhs.tonight)
}
print(sortedList)
输出:
[testStruct(今晚:“开放”,距离:34),testStruct(今晚:“开放”,距离:56),testStruct(今晚:“开放”,距离:94),testStruct(今晚:“关闭” ,距离:1),testStruct(今晚:“关闭”,距离:3),testStruct(今晚:“关闭”,距离:14),testStruct(今晚:“关闭”,距离:36),testStruct(今晚:“关闭“,距离:78)]