我想按升序对数组进行排序

时间:2013-02-13 06:36:14

标签: iphone objective-c sorting

数组:(2007-99,2001-96,2005-93)
分类输出应为:(2005-93,2001-96,2007-99)

请帮帮我。

4 个答案:

答案 0 :(得分:2)

您需要编写自定义比较器来执行此类操作。在下面的方法中,我使用rangeOfString获取破折号的位置,然后将字符串中的子字符串进一步放入字符串,然后将其转换为int以进行比较:

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"2007-07",@"2005-01",@"2004-09",@"2003-02", nil];
    NSArray *sortedArray = [arr sortedArrayUsingComparator: ^(NSString *s1, NSString *s2) {
        if ([[s1 substringFromIndex:[s1 rangeOfString:@"-"].location + 1] intValue] > [[s2 substringFromIndex:[s2 rangeOfString:@"-"].location + 1] intValue]) {
            return (NSComparisonResult)NSOrderedDescending;
        }

        if ([[s1 substringFromIndex:[s1 rangeOfString:@"-"].location + 1] intValue] < [[s2 substringFromIndex:[s2 rangeOfString:@"-"].location + 1] intValue]) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];

    NSLog(@"%@",sortedArray);

答案 1 :(得分:1)

您可以使用自定义块对此进行排序(请注意,我假设您的所有数字格式正确):

NSArray *rollNumbers = [NSArray arrayWithObjects:@"2007-99", @"2001-96", @"2005-93", nil];

NSArray *sortedRollNumbers = [rollNumbers sortedArrayUsingComparator:^NSComparisonResult(NSString *roll1, NSString *roll2) {
    NSArray *roll1Components = [roll1 componentsSeparatedByString:@"-"];
    NSArray *roll2Components = [roll2 componentsSeparatedByString:@"-"];
    NSNumber *roll1Number    = [NSNumber numberWithInt:[[roll1Components objectAtIndex:1] intValue]];
    NSNumber *roll2Number    = [NSNumber numberWithInt:[[roll2Components objectAtIndex:1] intValue]];
    return [roll1Number compare:roll2Number];
}];

NSLog(@"%@", sortedRollNumbers);

输出:

  

(       “2005-93”,       “2001-96”,       “2007-99”)

答案 2 :(得分:0)

您可以像这样对数组进行排序:

    NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"2007-07",@"2005-01",@"2004-09",@"2003-02", nil];
    NSMutableArray *marks = [[NSMutableArray alloc]init];

    for (int i = 0; i < arr.count; i++)
    {
        NSArray *sep = [[arr objectAtIndex:i] componentsSeparatedByString:@"-"];
        [marks addObject:[sep objectAtIndex:1]];
    }
    NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"" ascending:NO];
    [marks sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

    NSMutableArray *sortedFinalArray = [[NSMutableArray alloc]init];

    for (int i = 0; i < marks.count; i++)
    {
        for (int k = 0; k < arr.count; k++)
        {
            NSRange aRange = [[arr objectAtIndex:i] rangeOfString:[marks objectAtIndex:k]];
            if (!(aRange.location == NSNotFound))
            {
                [sortedFinalArray addObject:[arr objectAtIndex:k]];
            }
        }
    }

答案 3 :(得分:0)

为了对数组进行排序,必须成对地比较数组的元素以找出它们的顺序。您的具体订购是自定义的,因此您必须自己编写比较方法(例如名为compare:),然后您可以使用[arr sortUsingSelector:@selector(compare:)];对数组进行排序。
现在必须知道数组元素的compare:方法,因为每个元素都使用它来将它与同一个类的另一个元素进行比较。因此,要么为实现compare方法的元素定义一个新类,要么将它们保留为NSStrings,但是您必须定义一个实现compare:方法的类。 compare:方法本身看起来像这样(伪代码):

-(NSComparisonResult) compare: (MyString *) myString {
    if
        (self.the_last_2_characters_interpreted_asNumber <
             myString.the_last_2_characters_interpreted_asNumber)
                 return NSOrderedAscending;
    else if
        (self.the_last_2_characters_interpreted_asNumber ==
             myString.the_last_2_characters_interpreted_asNumber)
                 return NSOrderedSame;
    else
        return NSOrderedDescending;
}