Ios 在我的例子中,根据元素的时间戳对NSArray中的元素进行排序
我有一个Ios 在我的例子中,根据元素的时间戳对NSArray中的元素进行排序,ios,objective-c,sorting,nsarray,Ios,Objective C,Sorting,Nsarray,我有一个NSString数组,每个NSString元素包含一个时间戳(历元时间)和其他字符,例如:“时间:1474437948687, 因此,myData内部看起来像这样: {"time:1474437948687,fajlsfj...", "time:1474237943221, axsasdfd...", "time:1474681430940, someother...", ... } 我需要一个数组,它包含与上述数组相同的元素,但按时间戳的降序排序。我怎么做 我一直在迭代NSSt
NSString
数组,每个NSString
元素包含一个时间戳(历元时间)和其他字符,例如:“时间:1474437948687,
因此,myData
内部看起来像这样:
{"time:1474437948687,fajlsfj...",
"time:1474237943221, axsasdfd...",
"time:1474681430940, someother...",
...
}
我需要一个数组,它包含与上述数组相同的元素,但按时间戳的降序排序。我怎么做
我一直在迭代NSString数组:
for (NSString element in myData) {
...
}
首先,将字符串转换为以键值方式反映数据的实体类型的实例似乎是个好主意。然后,您可以使用
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中输入。使用以下排序器使用比较器进行排序它对我有效:我使用静态数据,例如时间: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];
}];
更新与基元类型的比较:
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];
}];
希望这能帮助你
如果您有任何其他疑问,请务必告诉我,尝试以下方法:
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..."
)
使用NSSortDescriptor根据时间缩短数组。谢谢,我很快就会看一看NSSortDescriptor,我以前没有用过它。你能给我看一下吗array@Leem.fin请检查我的答案,我在同一天用原语类型更新排序。谢谢,我现在正在尝试,我会告诉你结果的。我们能用以毫秒为单位的历元时间来做比较而不是转换成NSDate吗?我仍然在寻找。。。dateWithTimeIntervalSince1970将给出从1970年开始的毫秒。对于两个对象,因此需要比较两个对象给出的毫秒数。它可能无法解决比较两个日期和排序的目的…如果您想将历元转换为秒数的日期,那么我们可以使用不同的日期格式化程序。e、 g[df setDateFormat:@“dd-MM-yyyy H:MM:ss”];我设法通过转换为NSNumber来解决这个问题,并比较NSNumber对象
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..."
)