gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:59:02 27 4
gpt4 key购买 nike

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

@“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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com