如何使用 NSSortDescriptor 对 NSNumber 进行排序?

发布于 2024-08-05 16:25:46 字数 1411 浏览 8 评论 0原文

我在使用 NSSortDescriptor 对 NSNumber 进行排序时遇到问题。无论出于何种原因,它都不会按正确的顺序排序。我做错了什么?

- (NSNumber *)sortNumber {

NSNumber *sum = [NSNumber numberWithFloat:([self.capacity floatValue] / [self.availability floatValue])];

return sum;
}

它在 xcdataModel 中定义为 Int32,并由此类调用进行排序。

- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
    [fetchRequest setReturnsObjectsAsFaults:NO];  
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];
    NSArray *sortDescriptors = nil; 
} if ([fetchSectioningControl selectedSegmentIndex] == 0) {
        sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"sortNumber" ascending:YES selector:@selector(compare:)] autorelease]];
}
[fetchRequest setSortDescriptors:sortDescriptors];

编辑:是的,在寒冷的日子里,这需要更多的解释。我将尝试解释该项目。这是一个 CoreData 应用程序,值“容量”和“可用性”是通过使用 SAX 解析 XML 文件并将它们附加到托管对象模型而派生的,在托管对象模型中它们被定义为字符串,最初它们在 XML 中是数字。

然后将它们定义在一个类中,其中已进行上述计算并附加到同一对象模型(如果上面的代码是正确的,那么也许这就是问题所在?)。所有这些都是为了获得我想用来对 TableView 进行排序的百分比值。 (顺便说一句,我意识到他们需要交换,哎呀)第二位代码是使用 NSFetchResultsController 和 ManagedObjectContext 发生这种情况的地方。我知道这一点有效,因为我还通过设置为 if selectedSegmentIndex == 0 等的其他属性对该列表进行排序。它们都排序正确。

我希望这更有意义。

I'm having trouble sorting an NSNumber using NSSortDescriptor. For whatever reason it won't sort into the correct order. What am I doing wrong?

- (NSNumber *)sortNumber {

NSNumber *sum = [NSNumber numberWithFloat:([self.capacity floatValue] / [self.availability floatValue])];

return sum;
}

It is defined as an Int32 in the xcdataModel and called by this class to sort.

- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController == nil) {
    NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
    [fetchRequest setReturnsObjectsAsFaults:NO];  
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext]];
    NSArray *sortDescriptors = nil; 
} if ([fetchSectioningControl selectedSegmentIndex] == 0) {
        sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"sortNumber" ascending:YES selector:@selector(compare:)] autorelease]];
}
[fetchRequest setSortDescriptors:sortDescriptors];

EDIT:Yeah, in the cold light of day this needs a little more explanation. I'll try to explain the project. This is a CoreData app, the values 'capacity' and 'availability' are derived from parsing an XML file with SAX and attaching them to the Managed Object Model where they are defined as Strings, originally they would have been numeric in the XML.

These are then defined in a Class where the calculation above has been made and attached to the same Object Model (if the code above is right then perhaps this is where the problem is?). All this has been in effort to obtain a percentage value that I would like to use to sort a TableView. (BTW, I realise they need swapping around, oops) The second bit of code is where this happens using NSFetchResultsController and ManagedObjectContext. I know this bit works because I'm also sorting this list by other attributes set to if selectedSegmentIndex == 0 etc. They all sort correctly.

I hope this makes a bit more sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2024-08-12 16:25:46
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortNumber" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[请求setSortDescriptors:sortDescriptors];

您可以使用NSSortDescriptor而不调用compare方法。它会按照您想要的顺序自行排序。

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortNumber" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[request setSortDescriptors:sortDescriptors];

you can use NSSortDescriptor without calling compare method. It will sort itself in you desired order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文