简体   繁体   English

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

[英]How do you sort NSNumber with NSSortDescriptor?

I'm having trouble sorting an NSNumber using NSSortDescriptor. 我在使用NSSortDescriptor排序NSNumber时遇到问题。 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. 它在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];

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. 这是一个CoreData应用程序,值“ capacity”和“ availability”是通过使用SAX解析XML文件并将它们附加到定义为字符串的托管对象模型中而得出的,最初它们在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. 所有这些都是为了获得一个百分比值,我想用它来对TableView进行排序。 (BTW, I realise they need swapping around, oops) The second bit of code is where this happens using NSFetchResultsController and ManagedObjectContext. (顺便说一句,我意识到他们需要交换,哎呀)代码的第二位是使用NSFetchResultsController和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. 我知道此位是有效的,因为我还将按设置为selectedSegmentIndex == 0等的其他属性对该列表进行排序。它们都正确排序。

I hope this makes a bit more sense. 我希望这更有意义。

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

[request setSortDescriptors:sortDescriptors]; [request setSortDescriptors:sortDescriptors];

you can use NSSortDescriptor without calling compare method. 您可以使用NSSortDescriptor而不调用compare方法。 It will sort itself in you desired order. 它将按照您想要的顺序进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM