在我的案例

时间:2016-09-21 06:19:07

标签: ios objective-c sorting nsarray

我有一个NSString数组,每个NSString元素包含一个时间戳(纪元时间)和其他字符,例如:"time:1474437948687, <other characters>"

NSArrary *myData = [self loadData];// my array 

所以,myData内部看起来像这样:

{"time:1474437948687,fajlsfj...",
 "time:1474237943221, axsasdfd...",
 "time:1474681430940, someother...",
 ...
}

我需要一个包含与上面数组相同元素的数组,但是按时间戳的降序排序。我该怎么办?

我在迭代NSString数组时遇到困难:

for (NSString element in myData) {
 ...
}

3 个答案:

答案 0 :(得分:-1)

使用以下 sortedArrayUsingComparator 它对我有用:我使用静态数据,例如时间:1474437948687,..

**time:1474437948687, <other characters> Consider String Format..**

    NSArrary *myData = [self loadData];
    NSArrary *sortedmyData = [[myData sortedArrayUsingComparator: ^(id obj1, id obj2) {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        // Change Date formate accordingly ====
        [df setDateFormat:@"dd-MM-yyyy"];
        NSDate *d1 = [df dateFromString:[self sperateDate:obj1]];
        NSDate *d2 = [df dateFromString:[self sperateDate:obj2]];
        return [d1 compare: d2];
    }];

//这是根据时间:1474437948687开发的函数,考虑字符串格式..

- (NSString *)sperateDate : (NSString *)obj1 {
    NSArray *arr = [obj1 componentsSeparatedByString:@","];
    NSArray *arr1 = [arr[0] componentsSeparatedByString:@":"];
    return arr1[1];
}

更新与基本类型比较:

    NSArray *myData = @[@"time:1474437948687,fajlsfj...",@"time:1474237943221, axsasdfd...",@"time:1474681430940, someother..."
    NSArray *sortedmyData = [myData sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSNumber *d1 = [NSNumber numberWithDouble:[[self sperateDate:obj1] doubleValue]];
    NSNumber *d2 = [NSNumber numberWithDouble:[[self sperateDate:obj2] doubleValue]];
    return [d1 compare: d2];
    }];

希望这能帮到你......

如果您有任何其他疑问,请告诉我

答案 1 :(得分:-1)

试试这个:

   NSArray *timearray = @[@"time:1474437948687,fajlsfj...",
        @"time:1474237943221, axsasdfd...",
        @"time:1474681430940, someother..."];
    NSSortDescriptor *sortDescriptor;

    sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@""
                                                 ascending:NO];

    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray = [timearray sortedArrayUsingDescriptors:sortDescriptors];

    NSLog(@"sortedArray %@",sortedArray);

输出:

sortedArray (
        "time:1474681430940, someother...",
        "time:1474437948687,fajlsfj...",
        "time:1474237943221, axsasdfd..."
    )

答案 2 :(得分:-3)

首先,将字符串转换为以键值方式反映数据的实体类型的实例似乎是个好主意。然后,您可以使用NSSortDescriptor

的实例轻松对其进行排序
NSSortDescriptor *timeSorter = [NSSortDescriptor sortDescriptorWithKey:@"time" ascending:NO];
NSArray *sorted = [myData sortedArrayUsingSortDescriptors:@[timeSorter]];

但是,您可以使用更复杂的排序描述符对数组进行排序:

NSSortDescriptor *timeSorter = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO comparator:
^(id one, id two )
{
  NSString *timestamp1 = [one compenentsSepartedByString:@","][0];
  timestamp1 = [timestamp1 substringFromIndex:5];
  NSString *timestamp2 = [two compenentsSepartedByString:@","][0];
  timestamp2 = [timestamp2 substringFromIndex:5];
  return [timestamp1 compare:timestamp2 options:NSNumericSearch range:NSMakeRange(0, [timestamp1 length])];
}];
NSArray *sorted = [myData sortedArrayUsingSortDescriptors:@[timeSorter]];

输入Safari。