使用替换对另一个数组排序数组

时间:2013-05-14 01:14:22

标签: iphone ios objective-c c

我有一个天数(字符串格式),我的程序从用户输入接收到的长度介于1-7之间。收到的数组具有全文的日期名称。

我可能收到的数组示例是:["Tuesday", "Monday", "Thursday"]

我要做的是从Monday to Sunday对此数组进行排序,并将全文名称转换为缩写。因此,我对上述数组的排序函数最好返回:["M", "Tu", "Th"]

同一天的重复项将永远不会出现,永远不会少于1项且永远不会超过7项。

感谢。

这很粗糙,但这是用户从以下日期选择的粗略用户界面:

enter image description here

我使用了选定的答案,但将其改编为将其添加到我需要的地方。我改编如下:

-(NSArray*)array:(NSArray*)array collect:(id(^)(id object))block
{
    NSMutableArray * result = [ NSMutableArray array ] ;
    for( id object in array ) { [ result addObject:block( object) ] ; }
    return result ;
}

-(NSArray*)arrayBySortingAndAbbreviatingDayNames:(NSArray*)arrayToSort
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday",     @"Saturday", @"Sunday" ] ;
    NSArray * abbreviations = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSArray * array = [ self array:arrayToSort collect:^(NSString * dayName){
        return @([ dayNames indexOfObject:dayName ]) ;
    } ] ;
    array = [ array sortedArrayUsingSelector:@selector( compare: ) ] ;
    array = [ self array:array collect:^(NSNumber * index){
        return abbreviations[ [ index integerValue ] ] ;
    }];
    return array ;
}

2 个答案:

答案 0 :(得分:3)

@implementation NSArray (DayNameThing)

-(NSArray*)collect:(id(^)(id object))block
{
    NSMutableArray * result = [ NSMutableArray array ] ;
    for( id object in self ) { [ result addObject:block( object) ] ; }
    return result ;
}

-(NSArray*)arrayBySortingAndAbbreviatingDayNames
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * abbreviations = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSArray * array = [ self collect:^(NSString * dayName){
        return @([ dayNames indexOfObject:dayName ]) ;
    } ] ;
    array = [ array sortedArrayUsingSelector:@selector( compare: ) ] ;
    array = [ array collect:^(NSNumber * index){
        return abbreviations[ [ index integerValue ] ] ;
    }];
    return array ;
}

@end

所以...如果array包含用户选择的日期名称,您可以使用newArray = [ array arrayBySortingAndAbbreviatingDayNames ]获得所需的结果..


编辑想到了更好/更简单的方法:

NSArray * convertArray(NSArray * input)
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * shortDayNames = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;
    NSMutableArray * output = [ NSMutableArray array ] ;
    for( int index=0; index < 7; ++index )
    {
        if ( [ input containsObject:dayNames[ index ]] )
        {
            [ output addObject:shortDayNames[ index ]] ;
        }
    }
    return output ; 
}

编辑更好。

NSArray * ConvertArray(NSArray * input)
{
    NSArray * dayNames = @[ @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday" ] ;
    NSArray * shortDayNames = @[ @"M", @"Tu", @"W", @"Th", @"F", @"Sa", @"Su" ] ;

    return [ shortDayNames objectsAtIndexes:[ dayNames indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [ input containsObject:obj ] ;
    }]] ;
}

......好吧,我想我已经完成了。 :)

答案 1 :(得分:3)

这是另一种做同样事情的方法。

NSArray *input = @[@"Tuesday",@"Friday",@"Wednesday",@"Saturday"];

NSArray *dayArray = @[@{@"Monday":@"M"},@{@"Tuesday":@"Tu"},@{@"Wednesday":@"W"},@{@"Thursday":@"Th"},@{@"Friday":@"F"},@{@"Saturday":@"Sa"},@{@"Sunday":@"Su"}];
NSMutableArray *output = [@[@"",@"",@"",@"",@"",@"",@""] mutableCopy];
for (NSString *aDay in input) {
    NSInteger indx = [dayArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [[dayArray[idx] allKeys][0] isEqualToString:aDay];
    }];
    [output replaceObjectAtIndex:indx withObject:dayArray[indx][aDay]];
}
[output removeObjectIdenticalTo:@""];
NSLog(@"%@",output);