【问题标题】:Custom String Sort: How do I place these strings in an array in this particular order自定义字符串排序:如何按特定顺序将这些字符串放入数组中
【发布时间】:2016-06-29 23:36:02
【问题描述】:

/* 您好,我是 swift 新手,并且在自定义数组排序上遇到了困难。 如何按特定顺序将这些字符串放入数组中?

相机:Prime:滤镜:支持:

如果我使用 NSString,我可以使用 .containsString 我想要一个动态数组,因为我不知道列表最终会变得多大

感谢您查看我的问题! */

var equipmentList:[String]  = []

var item1   =   NSString(string: "Support: Fluid Head ")

var item2   =   NSString(string: "Prime: Ziess Master Primes")

var item3   =   NSString(string: "Filter: ND Set")

var item4   =   NSString(string: "Camera: Arri Alexa")



if item1.containsString("Camera:") {

    // place in first position in a dynamic array

}

if item1.containsString("Prime:") {

    // place in second position in a dynamic array

}

if item1.containsString("Filter:") {

    // place in third position in a dynamic array

}

if item1.containsString("Support:") {

    // place in forth position in a dynamic array

}

【问题讨论】:

  • 数组通常用作压缩列表。换句话说,它们本身没有位置,除了当前包含的元素定义的位置。您可以使用占位符对象(可能是 NSNull 或空字符串)来填充 4 元素数组,然后替换占位符,或者您可以选择不同的数据结构(如字典 - 无序,但由能够排序的东西键入,例如NSNumber)

标签: arrays swift string sorting nsstring


【解决方案1】:

试试这个。解释在 cmets 中:

// The order by which you will sort your equipment types
let order = ["Camera", "Prime", "Filter", "Support"]


// The sorting function
func compareEquipment(equipment1: String, equipment2: String) -> Bool {
    let components1 = equipment1.componentsSeparatedByString(":")
    let components2 = equipment2.componentsSeparatedByString(":")

    // Each equipment string must have exactly 2 components separeted by
    // a colon (:), like in this format:
    //      equipment type: eqipment name
    //
    // If not, quit the function reporting an error
    guard components1.count == 2 && components2.count == 2 else {
        fatalError("Invalid equipment: '\(equipment1)' or '\(equipment2)'")
    }

    // The order of the equipment type
    let order1 = order.indexOf(components1.first!) ?? Int.max
    let order2 = order.indexOf(components2.first!) ?? Int.max

    // When comparing 2 equipment strings, order them by equipment type
    // first. If they are of the same type, order them by name
    if order1 != order2 {
        return order1 < order2
    } else {
        return components1.last! < components2.last
    }
}


let equipmentList = [
    "Support: Fluid Head",
    "Prime: Ziess Master Primes",
    "Filter: ND Set",
    "Camera: Arri Alexa"
]

let sortedEquipmentList = equipmentList.sort(compareEquipment)

print(sortedEquipmentList)

【讨论】:

  • 这个解决方案..以及下面的一个都很好,谢谢你的帮助。我知道我哪里错了!!!!
【解决方案2】:

-- 编辑--

实际上回顾您的答案,不知道您真正想要什么,但更安全的解决方案是使用枚举器(用于自动完成并使用名称而不是数字)、设备类和设备列表:

enum EquipmentType : Int {
    case Camera  = 1
    case Prime = 2
    case Filter = 3
    case Support = 4
}

class Equipment {
    let equipmentType : EquipmentType
    let name : String

    init(type : EquipmentType, name : String) {
        self.equipmentType = type
        self.name = name
    }
}

class EquipmentList {
    var equipment : [Equipment] = []

    func sortEquipment() {
        self.equipment = self.equipment.sort({$0.equipmentType.rawValue < $1.equipmentType.rawValue})
    }

    func addEquipment(equipment : Equipment) {
        self.equipment.append(equipment)
        self.sortEquipment()
    }

    func addEquipmentList(equipmentList : [Equipment]) {
        self.equipment.appendContentsOf(equipmentList)
        self.sortEquipment()
    }

    func printEquipment() {
        self.equipment.forEach() { equipment in
            print("Type: \(equipment.equipmentType) - Name: \(equipment.name)")
        }
    }
}

let equipmentList = EquipmentList()

// Adding temp equipment
let item1 = Equipment(type: .Support, name: "Fluid Head")
let item2 = Equipment(type: .Prime, name: "Ziess Master Primes")
let item3 = Equipment(type: .Filter, name: "ND Set")
let item4 = Equipment(type: .Camera, name: "Arri Alexa")

equipmentList.addEquipmentList([item1, item2, item3, item4])
equipmentList.printEquipment()

我认为这将满足您的所有需求。

-- 原答案--

也许我不太了解这个问题,但要在数组中的特定索引处插入值,您可以使用以下方法:

Array.insert(Element, atIndex : Int)

要插入第 0、1、2、3 位,您只需调用:

equipmentList.insert(item1, atIndex: 0)
equipmentList.insert(item2, atIndex: 1)
equipmentList.insert(item3, atIndex: 2)
equipmentList.insert(item4, atIndex: 3)

如果要添加值时数组为空,也可以使用 append 方法将它们附加到数组中:

Array.append(Element)

如果数组为空,要追加到第 0、1、2、3 位,您可以调用:

equipmentList.append(item1)
equipmentList.append(item2)
equipmentList.append(item3)
equipmentList.append(item4)

【讨论】:

  • 这个解决方案..以及上面的一个都很好,谢谢你的帮助。我喜欢类和枚举的实现。我想过,但无法正确写出来。我看到你的解决方案是如何做到的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 2019-08-30
  • 2023-01-21
相关资源
最近更新 更多