NSSortDescriptor Nil Objects Parse

问题描述 投票:1回答:1

我正在我的应用程序中使用Parse,并且在获取对象时,我需要使用NSSortDescriptor。我可以通过特定的字段/属性进行排序,但是排序字段为nil的所有对象都将放在结果的第一位。

我是否可以让sort字段中的零个对象排在最后?

ios objective-c parse-platform nssortdescriptor
1个回答
1
投票

很难使零以升序排在最后。我的方法是以下一种:(a)您编写的基于块的NSComparator强制最后一次为空,或(b)使用NSSortDescriptor,但暂时或永久地将null替换为超出范围的最大值您的数据,或(c)使用NSSortDescriptor,简单排序,然后将空值手动移到末尾。

如果您还不喜欢排序描述符,我将按照比较器的想法(a)进行如下操作:(猜测您正在基于某些属性对PFObject进行排序)...

NSComparator comparator = ^(PFObject *objA, PFObject *objB) {
    id someProperyA = objA[@"someProperty"];
    id someProperyB = objB[@"someProperty"];

    // two nulls are equal
    if (!somePropertyA && !somePropertyB) return NSOrderedSame;

    // any single null is bigger in your world, so sort it last
    if (!somePropertyA) return NSOrderedDescending;
    if (!somePropertyB) return NSOrderedAscending;

    // otherwise use compare, or whatever you were planning to use
    // with your NSSortDescriptor
    return [somePropertyA compare:somePropertyB];
};
© www.soinside.com 2019 - 2024. All rights reserved.