Sorting Number using NSSortDescriptor
Asked Answered
Y

3

5

I got stuck in another problem again but after a long time.

This time I have database (Core Data), having an attribute of numbers which contains integer numbers like 213879,123,4,345,56567 and so.

I need to fetch data in ascending number order similar to like alphabetically order.

I am doing this in way given below,

fetchRequest.sortDescriptors=[NSArray arrayWithObject:
      [NSSortDescriptor sortDescriptorWithKey:@"numbers" 
                                    ascending:YES 
                                     selector:@selector(compare:)]];

but unfortunately it compares only the 1st digit of every number, mean if there are 2 numbers like 123 and 321, it will compare 1 (first digit of first number) with 3 (first digit of second number) and sort them.

It got confuse when there comes 123 and 111 (same first digits of all numbers).

If I am doing something wrong or the SortDescriptor works in this way? I need the solution to sort the numbers in ascending 123,133,213,451,516 likewise.

Thing to Remember In actual the attribute numbers will contains integer numbers having digits more then 6. e.g 1234567,234568,235481

Thanks to all who helps me a lot in anticipation.

Yokoyama answered 14/12, 2012 at 7:12 Comment(2)
First reaction was that you are comparing Strings, but now you said it is NSNumber, i suggest you to re-check by [obj className] it is says NSNumber or NSStringSepulchral
Please show you data model. If this was a number attribute, it would not be working like this. There is something missing from your question.Victorvictoria
E
3

It seems your attribute is of type NSString. Make sure you make it to NSNumber, the sorting will then work as expected.

There is no need to selector:@selector(compare:) in your sort descriptor as this is the default anyway.

Earleneearley answered 14/12, 2012 at 7:19 Comment(3)
Thanks for replaying soon. The attribute is of type NSNumber.Yokoyama
Please amend you question to: (1) show an example of an undesired sorting result and (2) the code you use to store the value of the attribute.Earleneearley
:: Thanks buddy, it worked fine. There was actually a little mistake in my Numbers having some wrong numbers that were causing the problem. I fixed the numbers and applied your solution and it worked very fine for me. Thanks again.Yokoyama
O
14

Try this:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc]initWithKey:@"rank"  ascending:YES selector:@selector(localizedStandardCompare:)];
Ocasio answered 27/2, 2014 at 8:32 Comment(2)
+1, localizedStandardCompare works good for numbers sorting using NSSortDescriptor in NSArray.Krp
I dont understand why but I have an objects with property @property (strong, nonatomic) NSNumber *price; and i got set this property with (i.e.) object.price = @(5727). And if i check if ([[object price] isKindOfClass:[NSString class]]) - this is true.Emee
E
3

It seems your attribute is of type NSString. Make sure you make it to NSNumber, the sorting will then work as expected.

There is no need to selector:@selector(compare:) in your sort descriptor as this is the default anyway.

Earleneearley answered 14/12, 2012 at 7:19 Comment(3)
Thanks for replaying soon. The attribute is of type NSNumber.Yokoyama
Please amend you question to: (1) show an example of an undesired sorting result and (2) the code you use to store the value of the attribute.Earleneearley
:: Thanks buddy, it worked fine. There was actually a little mistake in my Numbers having some wrong numbers that were causing the problem. I fixed the numbers and applied your solution and it worked very fine for me. Thanks again.Yokoyama
Y
3
[NSSortDescriptor sortDescriptorWithKey:@"self"
                              ascending:YES
                             comparator:^(id obj1, id obj2){
    return [(NSString*)obj1 compare:(NSString*)obj2
                            options:NSNumericSearch];
 }];

this is working fine for me perfect

Yardstick answered 8/1, 2013 at 4:40 Comment(1)
Problem is when you have SQLite store you cannot use comparator in NSSortDescriptor...Housemaid

© 2022 - 2024 — McMap. All rights reserved.