在Swift中交换数组值

时间:2014-10-31 06:39:05

标签: ios sorting swift

我在数组中有 3个元素,其中包含字符串数字(作为字符串)

例如,数组包含

等元素
 var elementArray:NSArray //"3","2","1","abc"

排序后变为

var sortedArray = //"1","2","3","abc"

我想首先创建一个字符串,最后创建数字。

预期输出应为

var sortedArray = //"abc","1","2","3"

如何交换数组元素。

请注意,如果数组有多个字符串和数字(​​"1""2"等字符串)。字符串(单词或字符)应该是第一个,数字(应该按升序排列)是最后的。

到目前为止,我试过这个。

 var sortedArray:NSArray = array.sortedArrayUsingComparator({(item1:AnyObject!, item2:AnyObject!) -> NSComparisonResult in
            let comparisonOptions:NSStringCompareOptions =
            NSStringCompareOptions.CaseInsensitiveSearch |
                NSStringCompareOptions.NumericSearch |
                NSStringCompareOptions.WidthInsensitiveSearch |
                NSStringCompareOptions.ForcedOrderingSearch;

            var str1:String = (item1 as String)
            var str2:String = (item2 as String)

            return str1.compare(str2, options: comparisonOptions, range: str1.startIndex ..< str1.endIndex, locale: NSLocale.currentLocale())
        })

将结果返回为"1","2","3","abc"

但预期输出应为"abc","1","2","3"。我怎样才能在数组中交换对象。

2 个答案:

答案 0 :(得分:1)

var elementArray = ["1","2","3","abc","def"]
sort(&elementArray){
    if $0.toInt() != nil && $1.toInt() != nil{
        return $0 < $1
    }
    else if $0.toInt() == nil && $1.toInt() == nil{
        return $0 < $1
    }
    else
    {
        return $0 > $1
    }
}

这应该是有效的 结果是 &#34; ABC&#34; &#34; DEF&#34; &#34; 1&#34; &#34; 2&#34; &#34; 3&#34;

如果结果abc 3 2 1正常,请使用此

var elementArray = ["1","2","3","abc","def"]
    sort(&elementArray,>)

这个结果是&#34; def&#34; &#34; ABC&#34; &#34; 3&#34; &#34; 2&#34; &#34; 1&#34;

更新

var elementArray:NSArray = ["1","2","3","abc","def"]
var sortedArray:NSArray = elementArray.sortedArrayUsingComparator({(item1:AnyObject!, item2:AnyObject!) -> NSComparisonResult in
    let comparisonOptions:NSStringCompareOptions =
    NSStringCompareOptions.CaseInsensitiveSearch |
        NSStringCompareOptions.NumericSearch |
        NSStringCompareOptions.WidthInsensitiveSearch |
        NSStringCompareOptions.ForcedOrderingSearch;

    var str1:String = (item1 as String)
    var str2:String = (item2 as String)
    if str1.toInt() != nil && str2.toInt() != nil{
        return str1.compare(str2, options: comparisonOptions, range: str1.startIndex ..< str1.endIndex, locale: NSLocale.currentLocale())
    }
    else if str1.toInt() == nil && str2.toInt() == nil{
        return str1.compare(str2, options: comparisonOptions, range: str1.startIndex ..< str1.endIndex, locale: NSLocale.currentLocale())
    }
    else
    {
        return str2.compare(str1, options: comparisonOptions, range: str1.startIndex ..< str1.endIndex, locale: NSLocale.currentLocale())
    }
})

答案 1 :(得分:1)

这是一种向Swift数组添加交换功能的方法:

public extension Array {
    mutating func swap(ind1: Int, _ ind2: Int){
        var temp: Element
        temp = self[ind1]
        self[ind1] = self[ind2]
        self[ind2] = temp
    }
}

现在,您可以按照以下方式使用:

let myArray = [[1], 2, "3"]
myArray.swap(0,2) // ["3", 2, [1]]