ios - 排序描述符不以字符串形式对数字进行排序 - iphone

标签 ios nssortdescriptor

我在核心数据中保存了一些字符串

@“123”、@“1”、@“432”、@“90003”、@“4567”、@“1002”

这需要升序排序,这是我编写的代码

NSFetchRequest *request1 = [[NSFetchRequest alloc] init];
            [request1 setEntity:[NSEntityDescription entityForName:@"ABCD" inManagedObjectContext:context]];
            [request1 setIncludesPendingChanges:NO];
            sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"statusIdNo" ascending:YES selector:@selector(localizedStandardCompare:)];
            [request1 setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

            statusArray = [context executeFetchRequest:request1 error:nil];

但是这段代码没有按照要求的方式对其进行排序。我在这里遗漏了什么吗?

编辑:

The Required Result : @"1" , @"123", @"432", , @"1002",  @"4567", @"90003"
Whats coming now : @"123"@"432",@"1"  , @"1002",   @"90003", @"4567"

我想补充的一件事是,该数组不包含原样的字符串。这些是核心数据实体对象。例如,它包含 X 类的学生对象,其中包含所有学生的学号、姓名和分数作为其属性。我们需要根据存储为 NSNumber 的卷号对其进行排序。

最佳答案

在您的情况下,使用 NSSortDescriptor 如下所示:

[NSSortDescriptor sortDescriptorWithKey:@"statusIdNo"
                              ascending:YES
                               selector:@selector(localizedStandardCompare:)];

有关此比较的更多信息如下:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html

编辑:

关于 https://developer.apple.com/library/mac/documentation/cocoa/reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html#jumpTo_29setInincludesPendingChanges

A value of YES is not supported in conjunction with the result type NSDictionaryResultType, including calculation of aggregate results (such as max and min). For dictionaries, the array returned from the fetch reflects the current state in the persistent store, and does not take into account any pending changes, insertions, or deletions in the context.

If you need to take pending changes into account for some simple aggregations like max and min, you can instead use a normal fetch request, sorted on the attribute you want, with a fetch limit of 1.

编辑2:

我的代码:

- (void) getAllObjects
{
    NSManagedObjectContext *context;
    context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];

    NSFetchRequest *request1 = [[NSFetchRequest alloc] init];
    [request1 setEntity:[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context]];
    [request1 setIncludesPendingChanges:NO];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"str" ascending:YES selector:@selector(localizedStandardCompare:)];
    [request1 setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    NSArray *statusArray = [context executeFetchRequest:request1 error:nil];

    for (Entity *ent in statusArray)
    {
        NSLog(@"%@", ent.str);
    }
}

结果:

2014-04-11 20:06:34.454 smth1[8918:60b] 1
2014-04-11 20:06:34.456 smth1[8918:60b] 123
2014-04-11 20:06:34.456 smth1[8918:60b] 432
2014-04-11 20:06:34.457 smth1[8918:60b] 1002
2014-04-11 20:06:34.457 smth1[8918:60b] 4567
2014-04-11 20:06:34.458 smth1[8918:60b] 90003

关于ios - 排序描述符不以字符串形式对数字进行排序 - iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23005107/

相关文章:

objective-c - 使用 NSSortDescriptor 通过 Core Data 对内部版本号进行排序

iOS:根据数组的顺序对文件夹中的文件进行排序

objective-c - NSFetchedResultsController 使用子属性排序

ios - RestKit - 一次处理一个 REST 操作

当我将 HTML 字符串(大量数据)加载到 WebView 时,iPhone 应用程序崩溃

ios - X代码 6 : I can't select a view by its storyboard ID

iphone - 在后台运行时接收可达性通知?

ios - Swift 相当于 `initWithContentsOfFile:` ?

iphone - 如何根据字典的几个属性对字典数组进行排序

objective-c - 按自定义对象的子对象属性对数组进行排序?