NSMutableArray的sortUsingSelector

主要使用sortUsingComparator,sortUsingFunction,sortUsingSelector进行排序。

1.头文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//
//  ArraryTest_OrderViewController.m
//  ArraryTest_Order
//
//  Created by lv wei on 13-4-27.
//  Copyright (c) 2013年 lv wei. All rights reserved.
//

#import "ArraryTest_OrderViewController.h"

@interface QStudent : NSObject

@property(nonatomic, retain) NSString* studentID;

-(NSComparisonResult) compare: (id)element;

@end

2.实现文件

  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
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

@implementation QStudent
@synthesize studentID = _studentID;

- (void)dealloc
{
    [_studentID release];
    [super dealloc];
}
//主要用来演示NSMutableArray的sortUsingSelector如何使用
- (NSComparisonResult)compare:(QStudent*)element
{
    int num1 = [_studentID intValue];
    int num2 = [element.studentID intValue];

    if (num1 == num2) {
        return NSOrderedSame;
    }
    else if (num1 < num2)
    {
        return NSOrderedAscending;
    }
    
    return NSOrderedDescending;
}

@end


@interface ArraryTest_OrderViewController ()
@property(nonatomic, retain)NSMutableArray* stringList;
@end

@implementation ArraryTest_OrderViewController
@synthesize stringList = _stringList;


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    NSMutableArray* stringList = [NSMutableArray arrayWithObjects:@"90", @"23",@"1",@"1000",@"0",@"99",nil];
    self.stringList = stringList;
    
    [self sortUsingComparator];
    [self sortComparatorWithOptions];
    [self sortUsingFuction];
    [self sortUsingSelector];
}

- (void)sortUsingComparator
{
    NSLog(@"sortUsingComparator");
    NSMutableArray* stringList = [_stringList mutableCopy];
    
    NSLog(@"原始的:%@",stringList);
    
    [stringList sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        if ([obj1 intValue] == [obj2 intValue]) {
            return NSOrderedSame;
        }
        else if([obj1 intValue] > [obj2 intValue] )
        {
            return NSOrderedDescending;
        }
        
        return NSOrderedAscending;
    }];
    
    NSLog(@"排序后:%@",stringList);
    
    [stringList release];
}

- (void)sortComparatorWithOptions
{
     NSLog(@"sortComparatorWithOptions");
    NSMutableArray* stringList = [_stringList mutableCopy];
    
    NSLog(@"原始的:%@",stringList);
    
    [stringList sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2) {
        
        if ([obj1 intValue] == [obj2 intValue]) {
            return NSOrderedSame;
        }
        else if([obj1 intValue] > [obj2 intValue] )
        {
            return NSOrderedDescending;
        }
        
        return NSOrderedAscending;
    }];
    
    
    
    NSLog(@"排序后:%@",stringList);
    
    [stringList release];
}

static NSComparisonResult ObjectCompare(id  obj1, id obj2, void* context)
{
    if ([obj1 intValue] == [obj2 intValue]) {
        return NSOrderedSame;
    }
    else if([obj1 intValue] > [obj2 intValue] )
    {
        return NSOrderedDescending;
    }
    
    return NSOrderedAscending;
}

- (void)sortUsingFuction
{
    NSLog(@"sortUsingFuction");
    NSMutableArray* stringList = [_stringList mutableCopy];
    
    NSLog(@"原始的:%@",stringList);
    
    [stringList sortUsingFunction:ObjectCompare context:nil];
    
    NSLog(@"排序后:%@",stringList);
    
    [stringList release];
}

- (void)sortUsingSelector
{
    NSLog(@"sortUsingSelector");
    const int KArrayLen = 6;
    int array[KArrayLen] = {90,23,1,1000,0,99};
    NSMutableArray* stringList = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < KArrayLen; ++i) {
        QStudent* student = [[QStudent alloc] init];
        student.studentID = [NSString stringWithFormat:@"%d",array[i]];
        [stringList addObject:student];
        [student release];
    }
   
    
    NSLog(@"原始的:");
    for (int i = 0; i < KArrayLen; ++i) {
    
        NSLog(@"%@",[[stringList objectAtIndex:i] studentID]);
    }
    
    [stringList sortUsingSelector:@selector(compare:)];
    
    NSLog(@"排序后:");
    
    for (int i = 0; i < KArrayLen; ++i) {
        
        NSLog(@"%@",[[stringList objectAtIndex:i] studentID]);
    }
    NSLog(@"反序后:");
    NSEnumerator *enumerator = [stringList reverseObjectEnumerator];
    [stringList release];
	stringList =[[NSMutableArray alloc]initWithArray: [enumerator allObjects]];
    
    for (int i = 0; i < KArrayLen; ++i) {
        
        NSLog(@"%@",[[stringList objectAtIndex:i] studentID]);
    }
    [stringList release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3.执行情况

 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

	2013-04-27 15:55:57.515 ArraryTest_Order[22759:11303] sortUsingComparator
	2013-04-27 15:55:57.516 ArraryTest_Order[22759:11303] 原始的:(
    90,
    23,
    1,
    1000,
    0,
    99
	)
	2013-04-27 15:55:57.517 ArraryTest_Order[22759:11303] 排序后:(
    0,
    1,
    23,
    90,
    99,
    1000
	)
	2013-04-27 15:55:57.517 ArraryTest_Order[22759:11303] sortComparatorWithOptions
	2013-04-27 15:55:57.517 ArraryTest_Order[22759:11303] 原始的:(
    90,
    23,
    1,
    1000,
    0,
    99
	)
	2013-04-27 15:55:57.518 ArraryTest_Order[22759:11303] 排序后:(
    0,
    1,
    23,
    90,
    99,
    1000
	)
	2013-04-27 15:55:57.518 ArraryTest_Order[22759:11303] sortUsingFuction
	2013-04-27 15:55:57.519 ArraryTest_Order[22759:11303] 原始的:(
    90,
    23,
    1,
    1000,
    0,
    99
	)
	2013-04-27 15:55:57.519 ArraryTest_Order[22759:11303] 排序后:(
    0,
    1,
    23,
    90,
    99,
    1000
	)
	2013-04-27 15:55:57.520 ArraryTest_Order[22759:11303] sortUsingSelector
	2013-04-27 15:55:57.520 ArraryTest_Order[22759:11303] 原始的:
	2013-04-27 15:55:57.520 ArraryTest_Order[22759:11303] 90
	2013-04-27 15:55:57.521 ArraryTest_Order[22759:11303] 23
	2013-04-27 15:55:57.521 ArraryTest_Order[22759:11303] 1
	2013-04-27 15:55:57.562 ArraryTest_Order[22759:11303] 1000
	2013-04-27 15:55:57.563 ArraryTest_Order[22759:11303] 0
	2013-04-27 15:55:57.563 ArraryTest_Order[22759:11303] 99
	2013-04-27 15:55:57.564 ArraryTest_Order[22759:11303] 排序后:
	2013-04-27 15:55:57.564 ArraryTest_Order[22759:11303] 0
	2013-04-27 15:55:57.564 ArraryTest_Order[22759:11303] 1
	2013-04-27 15:55:57.565 ArraryTest_Order[22759:11303] 23
	2013-04-27 15:55:57.565 ArraryTest_Order[22759:11303] 90
	2013-04-27 15:55:57.566 ArraryTest_Order[22759:11303] 99
	2013-04-27 15:55:57.566 ArraryTest_Order[22759:11303] 1000
	2013-04-27 15:55:57.566 ArraryTest_Order[22759:11303] 反序后:
	2013-04-27 15:55:57.567 ArraryTest_Order[22759:11303] 1000
	2013-04-27 15:55:57.567 ArraryTest_Order[22759:11303] 99
	2013-04-27 15:55:57.568 ArraryTest_Order[22759:11303] 90
	2013-04-27 15:55:57.568 ArraryTest_Order[22759:11303] 23
	2013-04-27 15:55:57.568 ArraryTest_Order[22759:11303] 1
	2013-04-27 15:55:57.569 ArraryTest_Order[22759:11303] 0