如何使用UnixTime标记对Array进行排序

时间:2013-12-12 12:20:36

标签: ios iphone sorting nsarray

我想按时间对数组进行排序。但问题是时间是在另一个数组(数组的子数组)中的Form UNIX时间戳中。

这是格式。请任何人帮我分类。

    (
    group,
    "The Comic Corner",
    ""
),
    (
    group,
    "Home Alone",
    ""
),
    (
    group,
    "Romantic Movies",
    ""
),
    (
    group,
    "Thriller Movies",
    ""
),
    (
    group,
    Badminton,
    ""
),
    (
    group,
    "Celebrity Gossip",
    ""
),
    (
    group,
    "Dark Night",
    ""
),
    (
    group,
    "Graphics Designer",
    ""
),
    (
    group,
    "I Just Wanna....",
    ""
),
    (
    group,
    Religion,
    ""
),
    (
    group,
    "English Movies",
    ""
),
    (
    group,
    "Music World",
    ""
),
    (
    group,
    "Actions Movies",
    ""
),
    (
    group,
    "Bed & Breakfast",
    ""
),
    (
    group,
    "Strategic Games",
    ""
),
    (
    group,
    "Beer Lovers",
    ""
),
    (
    group,
    Hackers,
    ""
),
    (
    group,
    "Darkroom-Darkroom",
    ""
),
    (
    group,
    "Arcade Games",
    ""
),
    (
    group,
    "Hindi Movies",
    ""
),
    (
    mood,
    Anxious,
    1386749684146
),
    (
    "profilepic_time",
    1386545975408
),
    (
    "status_msg",
    " *z32_ hell0",
    1386635654457
),
    (
    Broadcast,
    3,
    11,
    1372945566365
),
    (
    Broadcast,
    3,
    11,
    3
),
    (
    Broadcast,
    3,
    11,
    3
),
    (
    Broadcast,
    3,
    11,
    1372858799160
),
    (
    Broadcast,
    6,
    11,
    1372920363550
),
    (
    Broadcast,
    6,
    30,
    1373012360303
),
    (
    Broadcast,
    6,
    30,
    1373015204935
),
    (
    Broadcast,
    6,
    30,
    1373018547181
),
    (
    Broadcast,
    6,
    30,
    1373018768064
),
    (
    Broadcast,
    7,
    37,
    1373025360112
),
    (
    Broadcast,
    3,
    37,
    1373031270639
),
    (
    Broadcast,
    3,
    42,
    1373868972980
),
    (
    Broadcast,
    3,
    29,
    1372936645430
),
    (
    Broadcast,
    3,
    29,
    1372935983470
),
    (
    Broadcast,
    3,
    101,
    1374150527293
),
    (
    bulletin,
    "dbxjsbxdijxjdjxichdjdixhdbdjx hdjdbdh",
    1382331040766
),
    (
    bulletin,
    "aaabbb ccc dddd eee ffff",
    1382510132316
),
    (
    bulletin,
    "dvxishhxxbbosihdjxibididhxososjshsisos dijfjeosksn osjdjkd",
    1382510521856
),
    (
    bulletin,
    tsotdo,
    1382707279159
),
    (
    bulletin,
    84saurzuzri,
    1382707293764
),
    (
    bulletin,
    aiaktsktzyodld,
    1382707305875
),
    (
    bulletin,
    uarsistoso,
    1382707326326
),
    (
    bulletin,
    2e58e8etisististi,
    1382707335278
),
    (
    bulletin,
    ztkzktzgtkktotk,
    1382707484453
),
    (
    bulletin,
    ztkzktzgtkktotk9e85a4a47a48a37ariz,
    1382707491619
),
    (
    bulletin,
    "dbxidj didcxj",
    1384173984019
),
    (
    bulletin,
    "testing testing testing testing",
    1384589761276
),
    (
    bulletin,
    "testing testing testing testing",
    1384589772960
),
    (
    bulletin,
    "testing testing testing testing",
    1384589788706
),
    (
    bulletin,
    "testing testing testing testing",
    1384589822861
),
    (
    bulletin,
    "testing testing testing testing",
    1384589871332
),
    (
    bulletin,
    "testing testing testing testing",
    1384589875364
),
    (
    bulletin,
    "testing testing testing testing",
    1384589910927
),
    (
    bulletin,
    hey,
    1384589984774
),
    (
    bulletin,
    "heya ",
    1384590016235
),
    (
    bulletin,
    ok,
    1384597237610
),
    (
    bulletin,
    "hey ",
    1384605812061
),
    (
    bulletin,
    fhjfs,
    1384757502287
),

某些数组有时间戳,某些数组没有时间戳

这对我来说非常复杂。任何人都可以帮助我 感谢

1 个答案:

答案 0 :(得分:0)

您需要指定一个自定义比较器块,如下所示(您需要根据数据结构调整索引):

NSArray *array = @[@[@"a", @456], @[@"b", @123], @[@"c", @""]];

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(NSArray *a, NSArray *b) {
  if ([a.lastObject isKindOfClass:[NSNumber class]] &&
      [b.lastObject isKindOfClass:[NSNumber class]]) {
    return [a.lastObject compare:b.lastObject];
  }
  return NSOrderedAscending;
}];

NSLog(@"%@", sorted);

如果您希望非时间戳记录显示在带时间戳的记录之前,则可以将NSOrderedAscending更改为NSOrderedDescending

此外,我建议您避免将数据存储在NSArray的{​​{1}}中,并创建一个正确的DTO(可以定义它自己的排序功能,如果需要)你的物品。