1代码
//
// main.swift
// NSArrayDemo
//
// Created by 赵超 on 14-6-22.
// Copyright (c) 2014年 赵超. All rights reserved.
//
import Foundation
//创建NSArray
var array:NSArray=NSArray(object:"0")
//创建NSMutableArray
var arrays:NSMutableArray=NSMutableArray()
//增加数据
arrays.addObject("1")
arrays.addObject("2")
arrays.insertObject("99",atIndex:1)
arrays.addObjectsFromArray(array)
//遍历
println("排序前:")
for b in arrays{
print(b)
print(" ")
}
//排序
arrays.sortUsingComparator(
{
(s1:AnyObject!,s2:AnyObject!)->NSComparisonResult in
var str1=s1 as String
var str2=s2 as String
if str1<str2{
return NSComparisonResult.OrderedAscending
}else{
return NSComparisonResult.OrderedDescending
}
})
//遍历
println("
排序后:")
for b in arrays{
print(b)
print(" ")
}
2.结果
排序前:
1 99 2 0
排序后:
0 1 2 99 Program ended with exit code: 0