【问题标题】:how to sort Array with UnixTime stamp如何使用 Unix 时间戳对数组进行排序
【发布时间】:2013-12-12 12:20:36
【问题描述】:

我想按时间对数组进行排序。但问题是时间在另一个数组(数组的子数组)中的 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
),

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

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

【问题讨论】:

  • 查看 NSArray 的任何“sortedArrayUsing...”方法,以及 NSMutableArray 的“sortUsing...: 方法”。我通常发现对于我的片状大脑来说使用“.. .UsingFunction" 风格并为比较器定义 C 风格的函数,但其​​他人喜欢花哨的块的东西。
  • 它不是从 nsmutable 数组中给出的排序方法中排序的。我认为我们必须手动对其进行排序。但我找不到工作它的确切逻辑

标签: ios iphone sorting nsarray


【解决方案1】:

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

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

此外,我建议您避免将数据存储在NSArrays 的NSArray 中,并为您的项目创建一个适当的DTO(如果需要,可以定义它自己的排序函数)。

【讨论】:

  • 它正在排序,但我不知道在我的下一条评论中使用什么参数对排序后的字符串进行排序
  • (group, "Celebrity Gossip", "checkin_place", ""), ( bulletin, "testing testing testing testing", "checkin_place", 1384589875364), (广播, 3, 29, 1372936645430) , ( group, "Actions Movies", "checkin_place", ""), (Broadcast, 6, 30, 1373018768064), ( group, "Dark Night", "checkin_place", ""),
  • 如何正确排序日期请帮帮我
  • 我看到不同的记录有不同数量的元素。您对时间戳在哪里以及如何知道记录中是否不存在时间戳有任何承诺吗?
  • 如果我理解正确,那么最后一个对象是"" 或时间戳本身。我根据这个猜测编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2014-01-03
  • 2018-11-01
相关资源
最近更新 更多