首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift:如何对Array<String>排序

Swift:如何对Array<String>排序
EN

Stack Overflow用户
提问于 2015-10-12 15:28:35
回答 3查看 383关注 0票数 0

我有一个Array,其中包含7-4.json87-1.json102-4.json等值,并希望对其进行排序(升序)。我使用了以下代码:

代码语言:javascript
复制
var fileNames = ["7-4.json", "87-1.json", "102-4.json"]
fileNames = fileNames.sort{ $0 < $1 }
print(fileNames)

其结果是:

代码语言:javascript
复制
["102-4.json", "7-4.json", "87-1.json"]

所以不像我所想的那样起作用了。我怎么能把它分类成7-4,87-1,102-4?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-12 15:50:50

给你:

代码语言:javascript
复制
var fileNames = ["7-4.json", "87-1.json", "102-4.json"]

func sortWithCustomFormat(first: String, second: String) -> Bool{
    func extract(value: String) -> (Int, Int){
        return (Int(value.componentsSeparatedByString("-").first!)!, Int(value.componentsSeparatedByString("-").last!.componentsSeparatedByString(".").first!)!)
    }
    let firstNumber = extract(first)
    let secondNumber = extract(second)
    if firstNumber.0 != secondNumber.0 { return firstNumber.0 < secondNumber.0 }
    return firstNumber.1 < secondNumber.1
}

fileNames.sort(sortWithCustomFormat)

函数sortWithCustomFormat有一个函数extract,它接受输入的字符串并从中提取第一和第二数字。然后,你比较第一个数字。如果它们相等,那么比较第二个数字。

票数 4
EN

Stack Overflow用户

发布于 2015-10-12 15:58:31

代码语言:javascript
复制
var fileNames = ["87-1.json", "7-4.json", "87-3.json", "102-4.json"]
fileNames = fileNames.sort({ (s1, s2) -> Bool in

    let f1 = s1.stringByReplacingOccurrencesOfString(".json", withString: "")
    let f2 = s2.stringByReplacingOccurrencesOfString(".json", withString: "")

    let arr1 = f1.componentsSeparatedByString("-")
    let arr2 = f2.componentsSeparatedByString("-")


    var int1 = Int(arr1[0])
    var int2 = Int(arr2[0])

    if int1 < int2 {
        return true
    }
    else if int1 > int2 {
        return false
    }
    else {
        int1 = Int(arr1[1])
        int2 = Int(arr2[1])
        if int1 < int2 {
            return true
        }
        else if int1 > int2 {
            return false
        }
        else {
            return true
        }
    }
});
print(fileNames)
票数 0
EN

Stack Overflow用户

发布于 2015-10-12 16:34:04

试试这个..。

代码语言:javascript
复制
var fileNames = ["87-1.json", "7-4.json", "102-4.json"] 
// Modded OP's order to actually test sort
var sorted = fileNames.sort{ $0 < $1 }
print(sorted) // ["102-4.json", "7-4.json", "87-1.json"]
// Not sorted as OP "required", as they are string sorted, not number sorted

// Very simplistic solution
sorted = fileNames.sort { ($0 as NSString).integerValue < ($1 as NSString).integerValue}
print(sorted) // As OP requires, but...

// It won't sort on his count field - add a failing case...
fileNames = ["7-4.json", "87-1.json", "102-4.json", "102-1.json"]
sorted = fileNames.sort { ($0 as NSString).integerValue < ($1 as NSString).integerValue}
print(sorted) // ["7-4.json", "87-1.json", "102-4.json", "102-1.son"]
// WRONG!

// Define a simple function that parses his strings into tuples.
// This assumes that the Strings are valid, and fails safe if not.
// If you want more validation, add it yourself!
func myParse(s: String) -> (Int, Int) {
    let c = s.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "-."))
    switch c.count {
    case 0:
        print("Careful Will Robinson!")
        return (0, 0)
    case 1:
        print("Careful Will Robinson!")
        return ((c[0] as NSString).integerValue, 0)
    default:
        return ((c[0] as NSString).integerValue, (c[1] as NSString).integerValue)
    }
}

let test = fileNames.map { myParse($0) }
print("\(test)") // Test execution of function

sorted = fileNames.sort { (s1: String, s2: String) -> Bool in
    let t1 = myParse(s1)
    let t2 = myParse(s2)
    if t1.0 == t2.0 {
        return t1.1 < t2.1
    } else {
        return t1.0 < t2.0
    }
}
print(sorted) // As required ["7-4.json", "87-1.json", "102-1.json", "102-4.json"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33084820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档