本文主要是介绍swift NSMutableArray 排序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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(" ")
- }
2.结果
- 排序前:
- 1 99 2 0
- 排序后:
- 0 1 2 99 Program ended with exit code: 0
这篇关于swift NSMutableArray 排序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!