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("\n排序后:")
for b in arrays{
    print(b)
    print(" ")
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.



2.结果


排序前:
1 99 2 0 
排序后:
0 1 2 99 Program ended with exit code: 0
  • 1.
  • 2.
  • 3.
  • 4.