Displaying Records in Ascending order - Logic issue

Go To StackoverFlow.com

2

I have a JSON string as the following;

{
    "1": {
        "name": "Jerry",
        "age": "12"

    },
    "2": {
        "name": "Bob",
        "age": "16"

    "3": {.....
}

I have shown only 2 records but there will be a 100 of records in this JSON. There is an order in this 1,2,3 etc is the order number of the records.

I am ordering these records (as in displaying the 1st record, and then 2nd and 3rd and so forth). My code is provided below.

There is a problem here, when ever i have more than 10 records it doesn't display records in assending order. It first shows 1 and then 11,12,13... up to 19. Then it shows 2 and then 21,22,23 .... unto 29.

My code is as follows, how can i modify it to solve my issue ?

possible solution : if we could add a leading 0 in from of the numbers that are less than 10, it should filter out correctly. But i know that this is not the correct approach.

NSDictionary *dic = content;

            self.mutArr = [[NSMutableArray alloc] init];

            NSArray *array = [dic allKeys];
            NSArray * sortedArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

            for (NSString *str in sortedArray)
            {
                NSDictionary *dic2 = [dic objectForKey:str];
                Person *person = [[Person alloc] init];            
                person.nameOfRestaurant=[dic2 objectForKey:@"name"];
                person.personStartDate=[dic2 objectForKey:@"age"];

                [self.mutArr addObject:person];

            }

            self.personEntries = [NSArray arrayWithArray:self.mutArr];
2012-04-05 17:40
by user1315906


0

This is because you are sorting numbers lexicographically (i.e. as if they were words). You should provide your own implementation of the sort comparer that converts strings to integers, and only then compares them for ordering.

2012-04-05 17:44
by dasblinkenlight
Are there any tutorial/sample code that explains how to do this - user1315906 2012-04-05 17:46
@user1315906 The answer at this link provides a working example of doing what you are trying to do - dasblinkenlight 2012-04-05 17:47
Ads