对NSMutableArray进行排序

时间:2013-09-17 08:13:55

标签: iphone ios sorting nsmutablearray

在我的NSMutableArray我有这些需要排序的值,

+0.00,
+0.00,
+0.00,
+0.10,
+0.21,        
+3.04,
+3.45,
+3.56,
+4.87,
+5.01,
+5.05,
+5.40,
+6.07,
+6.57,
+6.71,
+7.54,
+7.58,
+8.11,
+8.94,
+9.42,
+9.49,
9.78,
-1.69,
-1.80,
-10.99,
-12.72,
-2.30,
-2.64,
-2.64,
-3.12,
-4.59,
-5.11,
-5.83,
-6.54,
-6.64

我的排序代码,

NSSortDescriptor * frequencyDescriptor = [[NSSortDescriptor alloc] initWithKey:ytd 
                                                                      ascending:YES];

id obj;
NSEnumerator * enumerator = [array objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);

NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
NSArray * sortedArray = [array sortedArrayUsingDescriptors:descriptors];

NSLog(@"\nSorted ...");
enumerator = [sortedArray objectEnumerator];
while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);

我得到的输出,

2013-09-17 13:00:28.315 Pondo MF[1275:c07] +0.00
2013-09-17 13:00:28.316 Pondo MF[1275:c07] +0.00
2013-09-17 13:00:28.316 Pondo MF[1275:c07] +0.00
2013-09-17 13:00:28.316 Pondo MF[1275:c07] +0.10
2013-09-17 13:00:28.317 Pondo MF[1275:c07] +0.21
2013-09-17 13:00:28.317 Pondo MF[1275:c07] +0.34
2013-09-17 13:00:28.317 Pondo MF[1275:c07] +0.68
2013-09-17 13:00:28.317 Pondo MF[1275:c07] +1.35
2013-09-17 13:00:28.318 Pondo MF[1275:c07] +18.59
2013-09-17 13:00:28.318 Pondo MF[1275:c07] +3.04
2013-09-17 13:00:28.318 Pondo MF[1275:c07] +3.45
2013-09-17 13:00:28.319 Pondo MF[1275:c07] +3.56
2013-09-17 13:00:28.319 Pondo MF[1275:c07] +4.87
2013-09-17 13:00:28.319 Pondo MF[1275:c07] +5.01
2013-09-17 13:00:28.319 Pondo MF[1275:c07] +5.05
2013-09-17 13:00:28.320 Pondo MF[1275:c07] +5.40
2013-09-17 13:00:28.320 Pondo MF[1275:c07] +6.07
2013-09-17 13:00:28.320 Pondo MF[1275:c07] +6.57
2013-09-17 13:00:28.320 Pondo MF[1275:c07] +6.71
2013-09-17 13:00:28.321 Pondo MF[1275:c07] +7.54
2013-09-17 13:00:28.321 Pondo MF[1275:c07] +7.58
2013-09-17 13:00:28.321 Pondo MF[1275:c07] +8.11
2013-09-17 13:00:28.322 Pondo MF[1275:c07] +8.94
2013-09-17 13:00:28.322 Pondo MF[1275:c07] +9.42
2013-09-17 13:00:28.322 Pondo MF[1275:c07] +9.49
2013-09-17 13:00:28.322 Pondo MF[1275:c07] +9.78
2013-09-17 13:00:28.323 Pondo MF[1275:c07] -1.69
2013-09-17 13:00:28.323 Pondo MF[1275:c07] -1.80
2013-09-17 13:00:28.323 Pondo MF[1275:c07] -10.99
2013-09-17 13:00:28.323 Pondo MF[1275:c07] -12.72
2013-09-17 13:00:28.324 Pondo MF[1275:c07] -2.30
2013-09-17 13:00:28.324 Pondo MF[1275:c07] -2.64
2013-09-17 13:00:28.324 Pondo MF[1275:c07] -2.64
2013-09-17 13:00:28.324 Pondo MF[1275:c07] -3.12
2013-09-17 13:00:28.325 Pondo MF[1275:c07] -4.59
2013-09-17 13:00:28.325 Pondo MF[1275:c07] -5.11
2013-09-17 13:00:28.325 Pondo MF[1275:c07] -5.83
2013-09-17 13:00:28.326 Pondo MF[1275:c07] -6.54
2013-09-17 13:00:28.326 Pondo MF[1275:c07] -6.64

这是不正确的,因为有序数组应以+18.59作为第一个值开头。我的代码在哪里出错了?有什么指针要修复它吗?

2 个答案:

答案 0 :(得分:3)

您可以使用NSComparator块对数组进行排序。这很简单:

[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    float f1 = [obj1 floatValue];
    float f2 = [obj2 floatValue];
    if(f1<f2)
        return NSOrderedDescending;
    else if(f1>f2)
        return NSOrderedAscending;
    else
        return NSOrderedSame;
}];

在这种情况下,您的数组将按降序排序。如果你想以升序排序,只需在块中交换NSOrderedDescending和NSOrderedAscending。

答案 1 :(得分:2)

假设你的数字是字符串,我创建了一个函数,显示如何将这些字符串转换为NSNumbers,然后使用&#34; compare:&#34;进行排序。选择器:

- (void) demonstrateSorting
{
    NSArray * unsortedArray = @[ @"+0.00",@"+0.00",@"+0.00",@"+0.10",@"+0.21", \
                                 @"+3.04",@"+3.45",@"+3.56",@"+4.87",@"+5.01",@"+5.05",@"+5.40",@"+6.07",@"+6.57",@"+6.71",\
                                 @"+7.54",@"+7.58",@"+8.11",@"+8.94",@"+9.42",@"+9.49",@"9.78",@"-1.69",@"-1.80",\
                                 @"-10.99",@"-12.72",@"-2.30",@"-2.64",@"-2.64",@"-3.12",@"-4.59",@"-5.11",\
                                 @"-5.83",@"-6.54",@"-6.64"];

    NSMutableArray * arrayOfNumbers = [[NSMutableArray alloc] initWithCapacity: [unsortedArray count]];
    for(NSString * someNumberString in unsortedArray)
    {
        NSNumber * number = [NSNumber numberWithFloat:[someNumberString floatValue]];
        [arrayOfNumbers addObject: number];
    }

    id obj;
    NSEnumerator * enumerator = [arrayOfNumbers objectEnumerator];
    while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);

    NSArray * sortedArray =
    [arrayOfNumbers sortedArrayUsingSelector:@selector(compare:)];

    NSLog(@"\nSorted ...");

    enumerator = [sortedArray objectEnumerator];
    while ((obj = [enumerator nextObject])) NSLog(@"%@", obj);
}