本文介绍了在 NSArray 内的 NSDictionary 内对 NSDates 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在我的应用程序中,我有一个从 NSUserDefaults 加载的 NSArray.NSArray 中的每个对象都是一个 NSDictionary,它有大约 5 个不同的键.我根据其中的 NSDates 重新排序 NSDictionarys 本身.我不想单独对 NSDates 进行排序.

Currently in my app I have a NSArray that I load from NSUserDefaults. Each object in the NSArray is a NSDictionary that has about 5 different keys. I am re-order the NSDictionarys themselves based upon the NSDates inside of them. I do not want to sort the NSDates alone.

我已尝试使用此代码执行此操作:

I have tried to do this with this code:

NSComparator sortByDate = ^(id dict1, id dict2) {
        NSDate* n1 = [dict1 objectForKey:@"Date"];
        NSDate* n2 = [dict2 objectForKey:@"Date"];
        return (NSComparisonResult)[n1 compare:n2];
    };
    [self.cellArray sortUsingComparator:sortByDate];

即使代码被执行,也没有任何反应.我对如何根据其中的内容重新排序 NSDictionarys 感到困惑.

Although nothing happens even though the code gets executed. I am confused though about how to re-order the NSDictionarys based upon something inside of it.

任何人都可以对此提供任何见解吗?

Can anyone offer any insight on this?

谢谢!

推荐答案

您可以尝试对其进行内联排序,但我怀疑这会产生任何重大影响.但我可以说我从不抽象出我的块.我肯定也会尝试 NSLogging 以确保您获得有效的 NSDates.

You could try sorting it inline but I doubt that would have any major effect. But I can say i never abstract out my blocks. I would definitely try NSLogging too to make sure you're getting valid NSDates.

    [cells sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSDate *n1 = [obj1 objectForKey:@"Date"];
        NSDate *n2 = [obj2 objectForKey:@"Date"];

        if (n1 > n2) {
            return NSOrderedAscending;
        } else (n1 < n2) {
            return NSOrderedDescending;
        } else {
            return NSOrderedSame;
        }

    }];

这篇关于在 NSArray 内的 NSDictionary 内对 NSDates 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 20:35