Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用NSSortDescriptor在排序末尾放置空白字符串的简单方法?_Ios_Sorting_Nsdictionary_Nssortdescriptor - Fatal编程技术网

Ios 使用NSSortDescriptor在排序末尾放置空白字符串的简单方法?

Ios 使用NSSortDescriptor在排序末尾放置空白字符串的简单方法?,ios,sorting,nsdictionary,nssortdescriptor,Ios,Sorting,Nsdictionary,Nssortdescriptor,我正在使用NSSortDescriptor对NSDictionary项的NSArray进行排序。很好,正是我需要的。。。除了我希望在排序列表的末尾显示我的排序键有空值的字典项。有没有一种方法可以轻松实现这一点?还是我必须创建一些自定义排序函数?不,我不想把它设置为DESC order。。。我希望排序结果看起来像A、A、B、B、C、blanks、blanks。使用NSNumericSort而不是NSDiacriticInsensitive或NSCaseSensitive进行排序。由于ascii码中

我正在使用
NSSortDescriptor
NSDictionary
项的
NSArray
进行排序。很好,正是我需要的。。。除了我希望在排序列表的末尾显示我的排序键有空值的字典项。有没有一种方法可以轻松实现这一点?还是我必须创建一些自定义排序函数?不,我不想把它设置为DESC order。。。我希望排序结果看起来像A、A、B、B、C、blanks、blanks。

使用NSNumericSort而不是NSDiacriticInsensitive或NSCaseSensitive进行排序。由于ascii码中的空格可能是256,它将被发送到列表的后面。

编辑:我误读了这个问题,并认为其目的是将带前导空格的字符串按排序顺序放在第一位(无论如何,它们都应该如此)。

我想你想要这样的东西。这是完全未经测试的,不考虑多个前导空格。我相信有一种更优雅的方式,但它应该能完成任务

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {
    NSString *str1 = (NSString *)obj1;
    NSString *str2 = (NSString *)obj2;

    BOOL str1HasSpace = [str1 hasPrefix:@" "];
    BOOL str2HasSpace = [str2 hasPrefix:@" "];

    if (str1HasSpace && !str2HasSpace) {
        return NSOrderedAscending;
    } else if (str2HasSpace && ! str1HasSpace) {
        return NSOrderedDescending;
    } else {
        return [[str1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] compare:[str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
    }
}];

在看到另一个使用自定义比较的示例后,我们发现了这一点。以下是我最后得到的代码:

@interface NSString (CustomStatusCompare)
- (NSComparisonResult)customStatusCompare:(NSString*)other;
@end

@implementation NSString (CustomStatusCompare)
- (NSComparisonResult)customStatusCompare:(NSString*)other {
    NSAssert([other isKindOfClass:[NSString class]], @"Must be a NSString");
    if ([self isEqual:other]) {
        return NSOrderedSame;
    }
    else if ([self length] > 0 && [other length] > 0) {
        return [self localizedCaseInsensitiveCompare:other];        
    }
    else if ([self length] > 0 && [other length] == 0) {
        return NSOrderedAscending;
    }
    else {
        return NSOrderedDescending;
    } 
}
@end



NSSortDescriptor *serviceTypeDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"Service"
                             ascending:YES
                             selector:@selector(localizedCaseInsensitiveCompare:)];

NSSortDescriptor *locationDescriptor = 
[[NSSortDescriptor alloc] initWithKey:@"Location"
                             ascending:YES
                             selector:@selector(customStatusCompare:)];  //using custom comparison here!

NSArray *descriptors = [NSArray arrayWithObjects:locationDescriptor, nameDescriptor, nil];        
self.navArray = [self.navArray sortedArrayUsingDescriptors:descriptors];

因此,如果两个字符串都为空,比较器将返回OrderedName。。。如果两个字符串均为非空,则调用常规比较函数。。。如果只有一个字符串为空,则会反转该比较的正常顺序。瞧

你所说的空白值是什么意思?没有值、空字符串、空白或
NSNull
?我指的是空字符串。从plist加载NSDictionary,该值来自:。谢谢,我知道您要到哪里。我最后做的事情非常相似。。。将此自定义比较放在NSString类别中自己的方法中(这是正确的名称吗?)而不是块中。哇,对不起,我完全误解了您的问题,并认为您希望先使用带前导空格的字符串:/I将继续阅读理解……请注意,这在核心数据的编译谓词中不起作用:/