Question

I have a json response below and need to sort the array depending on the 'order' key and show it to the subView.

[
    {
        "ProjectID": 1,
        "SLNO": 1,
        "ID": 1,
        "Type": "Text",
        "Name": "First Name",
        "Order": 1,
        "Flag": "F"
    },
    {
        "ProjectID": 1,
        "SLNO": 3,
        "ID": 2,
        "Type": "Text",
        "Name": "Company",
        "Order": 5,
        "Flag": "F"
    },
    {
        "ProjectID": 1,
        "SLNO": 4,
        "ID": 4,
        "Type": "Text",
        "Name": "Personal Email",
        "Order": 3,
        "Flag": "F"
    },
    {
        "ProjectID": 1,
        "SLNO": 2,
        "ID": 8,
        "Type": "Text",
        "Name": "Last Name",
        "Order": 2,
        "Flag": "F"
    }
]

I read this documentation but how will store and sort the 'tempArray' to the custom class 'SaveAsking' in my following code. I am aware I need to use initWithKey:@"Order", but how in the following case?

NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:Nil];

        //NSArray *arrayLabel = [[NSArray alloc] initWithObjects:label1, label2, label3, label4, label5, label6, nil];


        NSMutableArray *arrayOrder = [NSMutableArray arrayWithCapacity:myJsonArray.count];
        NSMutableArray *arrayName = [NSMutableArray arrayWithCapacity:myJsonArray.count];
        i = 0;
        for(NSDictionary *myJsonDictionary in myJsonArray)
        {
            //UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++];
            //[label setText:myJsonDictionary[@"Name"]];

            NSString *name = myJsonDictionary[@"Name"];
            NSLog(@"Question from ws2 is %@", name);

            projectIdGobal = myJsonDictionary[@"ProjectID"];
            NSLog(@"Project id from ws2 is %@", projectIdGobal);

            slno = myJsonDictionary[@"SLNO"];
            NSLog(@"slno from ws2 is %@", slno);

            NSString *idWS2 = myJsonDictionary[@"ID"];
            NSLog(@"id from ws2 is %@", idWS2);

            order = myJsonDictionary[@"Order"];
            NSLog(@"order from ws2 is %@", order);

            flag = myJsonDictionary[@"Flag"];
            NSLog(@"flag from ws2 is %@", flag);



            [self putLabelsInScrollView:name:order]; //need to call this method depending on the sorted 'order' key


            SaveAsking *save = [[SaveAsking alloc] initWithslno:slno withOrder:order withFlag:flag];
            save.slno = slno;
            save.order = order;
            save.flag = flag;

            [temporaryArray addObject:save];
            //temporaryArray = [[NSMutableArray alloc] initWithObjects:slno, order, flag, nil];

            [arrayOrder addObject:order];
            [arrayName addObject:name];

            i++;
        }



        //Sort array or orders

        sortedArrayOfOrder = [arrayOrder sortedArrayUsingComparator:^(id str1, id str2) {

            if ([str1 integerValue] > [str2 integerValue]) {
                return (NSComparisonResult)NSOrderedDescending;
            }

            if ([str1 integerValue] < [str2 integerValue]) {
                return (NSComparisonResult)NSOrderedAscending;
            }
            return (NSComparisonResult)NSOrderedSame;
        }];
        NSLog(@"Array or orders = %@", arrayOrder);
        NSLog(@"Sorted array of orders = %@", sortedArrayOfOrder);
        //NSLog(@"Array from ws2 = %@", temporaryArray);
        NSLog(@"Array in myJsonArray = %@", myJsonArray);
        NSLog(@"Number of cycles in for-each = %d", i);


    }
Was it helpful?

Solution

I think you can directly sort myJsonArray by sortUsingComparator on the json object.

So

NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:Nil];

NSArray sortedArrayOfOrder = [arrayOrder sortedArrayUsingComparator:^(NSDictionary json1, NSDictionary json2) {
    if ([[json1 objectForKey:@"Order"] integerValue] > [[json2 objectForKey:@"Order"] integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([[json1 objectForKey:@"Order"] integerValue] < [[json2 objectForKey:@"Order"] integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top