自定义字符串排序:如何以特定顺序将这些字符串放在数组中

时间:2016-06-29 23:36:02

标签: arrays swift string sorting nsstring

/ * 您好,我是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

}

2 个答案:

答案 0 :(得分:1)

试一试。说明在评论中:

// 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)

答案 1 :(得分:0)

- 编辑 -

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

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)