Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我如何在swift中仅重新订购部分集合?_Ios_Arrays_Swift_Sorting - Fatal编程技术网

Ios 我如何在swift中仅重新订购部分集合?

Ios 我如何在swift中仅重新订购部分集合?,ios,arrays,swift,sorting,Ios,Arrays,Swift,Sorting,我有一个项的数组。由于集合是基于其他地方的大量用户操作从API提供的,因此无法保证顺序 但是,我需要按类型字段对该集合进行排序,以便在其他地方正确地对其进行迭代 在下面的示例中,我需要按照以下逻辑重新排序的集合: [ Item(type: .foo), Item(type: .bar), Item(type: .boo), Item(type: .baz), Item(type: .boom), Item(type: .bang) ] 具体地说,.foo和.bar

我有一个
项的数组
。由于集合是基于其他地方的大量用户操作从API提供的,因此无法保证顺序

但是,我需要按
类型
字段对该集合进行排序,以便在其他地方正确地对其进行迭代

在下面的示例中,我需要按照以下逻辑重新排序的集合:

[
  Item(type: .foo),
  Item(type: .bar),

  Item(type: .boo),
  Item(type: .baz),

  Item(type: .boom),
  Item(type: .bang)

]
具体地说,
.foo
.bar
应该是前两项,
.boom
bang
将是最后两项

这些固定项是唯一的,响应中不存在重复项

所有剩余的东西都应该在这两组之间,顺序与原始收藏中的顺序相同

我曾尝试在
var output:[item]
中将该项拆分为两个集合,并在索引处插入第二个集合,但排序仍处于关闭状态

我怎样才能做到这一点

导入UIKit
枚举项类型:字符串{
凯斯福
箱杆
案例boo
案例baz
箱梁
案例爆炸
}
结构项{
let类型:ItemType
}
让道具=[
项目(类型:.bang),
项目(类型:.bar),
项目(类型:.baz),
项目(类型:.boo),
项目(类型:。吊杆),
项目(类型:.foo)
]
/*
凯斯福
箱杆
>------------应按照与“道具”中相同的顺序订购`
箱梁
案例爆炸
*/
变量输出:[项目]{
变量fixedPosition:[项目]=[]
变量dynamicPosition:[项目]=[]
props.forEach{中的项
如果(item.type=.foo | | item.type=.bar | | item.type=.boom | | item.type=.bang){
fixedPosition.append(项目)
}否则{
dynamicPosition.append(项)
}
}
变量结果:[项目]=固定位置
结果.插入(内容:dynamicPosition,at:1)
返回结果
}
打印(output.map{$0.type})

您可以为每个枚举案例指定一个int值,并对其进行稳定排序:

func intValue(for itemType: ItemType) -> Int {
    switch itemType {
    case .foo:
        return 0
    case .bar:
        return 1
    case .boo, .baz: // the order of these should remain the same, so they get the same number
        return 2
    case .boom:
        return 3
    case .bang:
        return 4
    }
}

let sorted = props.stableSorted(by: { intValue(for: $0.type) < intValue(for: $1.type) })

您可以为每个枚举案例指定一个int值,并对其进行稳定排序:

func intValue(for itemType: ItemType) -> Int {
    switch itemType {
    case .foo:
        return 0
    case .bar:
        return 1
    case .boo, .baz: // the order of these should remain the same, so they get the same number
        return 2
    case .boom:
        return 3
    case .bang:
        return 4
    }
}

let sorted = props.stableSorted(by: { intValue(for: $0.type) < intValue(for: $1.type) })

随着
ItemType
实施
可比
协议的变化:

extension ItemType: Comparable {
    static func < (lhs: ItemType, rhs: ItemType) -> Bool {
        // function returns `true` iff lhs is smaller than res

        // order of cases matters here, switch matches them left-to-right, top-to-bottom
        switch (lhs, rhs) {
        case (.foo, _): // .foo is before everyone else
            return true
        case (_, .foo): // nothing is "smaller" than .foo
            return false
        case (.bar, _):  // .bar can be preceded only by .foo (previous 2 cases handle it)
            return true
        case (_, .bang): // .bang is always last
            return true
        case (.bang, _): // nothing is "bigger" than .bang
            return false
        case (_, .boom): // .boom can be only before .bang (previous 2 cases handle it)
            return true
        default: // otherwise we don't care about comparing items, neither item is "smaller" than other
            return false
        }
    }
}
扩展项类型:可比较{
静态函数<(左:项目类型,右:项目类型)->Bool{
//当lhs小于res时,函数返回'true'
//箱子的顺序在这里很重要,开关从左到右,从上到下匹配
开关(左、右){
案例(.foo,quo)://.foo比其他人都重要
返回真值
case(u,.foo)://没有比.foo“小”的
返回错误
案例(.bar,551;):/.bar前面只能有.foo(前两个案例处理)
返回真值
case(u,.bang)://bang总是最后一个
返回真值
case(.bang,u)://没有比.bang更“大”的了
返回错误
案例(u,.boom)://.boom只能在.bang之前(前两个案例处理)
返回真值
default://否则我们不关心比较项目,两个项目都不比其他项目“小”
返回错误
}
}
}

随着
项目类型的变化
实施
可比
协议:

extension ItemType: Comparable {
    static func < (lhs: ItemType, rhs: ItemType) -> Bool {
        // function returns `true` iff lhs is smaller than res

        // order of cases matters here, switch matches them left-to-right, top-to-bottom
        switch (lhs, rhs) {
        case (.foo, _): // .foo is before everyone else
            return true
        case (_, .foo): // nothing is "smaller" than .foo
            return false
        case (.bar, _):  // .bar can be preceded only by .foo (previous 2 cases handle it)
            return true
        case (_, .bang): // .bang is always last
            return true
        case (.bang, _): // nothing is "bigger" than .bang
            return false
        case (_, .boom): // .boom can be only before .bang (previous 2 cases handle it)
            return true
        default: // otherwise we don't care about comparing items, neither item is "smaller" than other
            return false
        }
    }
}
扩展项类型:可比较{
静态函数<(左:项目类型,右:项目类型)->Bool{
//当lhs小于res时,函数返回'true'
//箱子的顺序在这里很重要,开关从左到右,从上到下匹配
开关(左、右){
案例(.foo,quo)://.foo比其他人都重要
返回真值
case(u,.foo)://没有比.foo“小”的
返回错误
案例(.bar,551;):/.bar前面只能有.foo(前两个案例处理)
返回真值
case(u,.bang)://bang总是最后一个
返回真值
case(.bang,u)://没有比.bang更“大”的了
返回错误
案例(u,.boom)://.boom只能在.bang之前(前两个案例处理)
返回真值
default://否则我们不关心比较项目,两个项目都不比其他项目“小”
返回错误
}
}
}

如果原始数组有多个
foo
bar
,该怎么办?对不起,我应该说清楚,“固定”项是唯一的,它们不会重复。如果原始数组有多个
foo
bar
,该怎么办?对不起,我应该说清楚,“固定”项是唯一的,它们不会被复制。即使不进行
intValue
casting,也可以进行复制。您只需将
ItemType
设置为可比较的
,并且在函数
@user28434中,是的,但是
ItemType
具有可比较性可能没有意义。而且我无法实现
,即使没有
intValue
casting也可以实现。您只需将
ItemType
设置为可比较的
,并且在函数
@user28434中,是的,但是
ItemType
具有可比较性可能没有意义。我无法实现