Shivam Tripathi
Shivam Tripathi

Reputation: 1493

How to get all values from dictionary in ordered manner

I am getting a JSON like this :

{
    "count": {
        "2018-03-26 07": 4,
        "2018-03-26 08": 21,
        "2018-03-26 09": 34,
        "2018-03-26 10": 43,
        "2018-03-26 11": 64,
        "2018-03-26 12": 48,
        "2018-03-26 13": 31,
        "2018-03-26 14": 45,
        "2018-03-26 15": 48,
        "2018-03-26 16": 45,
        "2018-03-26 17": 40,
        "2018-03-26 18": 44,
        "2018-03-26 19": 58,
        "2018-03-26 20": 49,
        "2018-03-26 21": 60,
        "2018-03-26 22": 46,
        "2018-03-26 23": 35,
        "2018-03-27 00": 34,
        "2018-03-27 01": 36,
        "2018-03-27 02": 16,
        "2018-03-27 03": 23,
        "2018-03-27 04": 14,
        "2018-03-27 05": 12,
        "2018-03-27 06": 12,
        "2018-03-27 07": 1
    }
}

Now to get all values from this dict I tried [dict allValues] but it is returning dictionary in unordered format. It will take more effort to create these keys manually and get all the values in ordered manner. What i want is all these value (4,21,34,...) in the same order as they are coming because I have to use these values to draw some kind of graph. Is there a better way to do this?

Upvotes: 0

Views: 126

Answers (3)

Jabson
Jabson

Reputation: 1703

use dictionary instance method sorted like this:

let dict = ["A": 123, "B": 789, "C": 567, "D": 432]
let dictValInc = dict.sorted(by: { $0.value < $1.value })

result will be

[(key: "A", value: 123), (key: "D", value: 432), (key: "C", value: 567), (key: "B", value: 789)]

in objective-c you can sort dictionary keys using there values

NSDictionary *dict = @{@"A": @(123), @"B": @(789), @"C": @(567), @"D": @(432)};
NSArray *keys = [dict allKeys];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSString *first = [dict objectForKey:a];
    NSString *second = [dict objectForKey:b];
    return [first compare:second];
}];

and finnaly if you want your array be sorted by their keys:

 NSArray *keys = [dict allKeys];
 NSArray *sortedKeys = [keys sortedArrayUsingComparator:^(id a, id b) {
     return [a compare:b options:NSNumericSearch];
 }];

Upvotes: 2

Larme
Larme

Reputation: 26096

NSString *jsonStr = @"{\"count\":{\"2018-03-26 07\":4,\"2018-03-26 08\":21,\"2018-03-26 09\":34,\"2018-03-26 10\":43,\"2018-03-26 11\":64,\"2018-03-26 12\":48,\"2018-03-26 13\":31,\"2018-03-26 14\":45,\"2018-03-26 15\":48,\"2018-03-26 16\":45,\"2018-03-26 17\":40,\"2018-03-26 18\":44,\"2018-03-26 19\":58,\"2018-03-26 20\":49,\"2018-03-26 21\":60,\"2018-03-26 22\":46,\"2018-03-26 23\":35,\"2018-03-27 00\":34,\"2018-03-27 01\":36,\"2018-03-27 02\":16,\"2018-03-27 03\":23,\"2018-03-27 04\":14,\"2018-03-27 05\":12,\"2018-03-27 06\":12,\"2018-03-27 07\":1}}";
NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

NSDictionary *countDict = jsonDict[@"count"];

NSArray *keys = [countDict allKeys];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH"];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull dateStr1, NSString * _Nonnull dateStr2) {
    NSDate *date1 = [dateFormatter dateFromString:dateStr1];
    NSDate *date2 = [dateFormatter dateFromString:dateStr2];
    return [date1 compare:date2];
}];

NSMutableArray *valuesSorted = [[NSMutableArray alloc] init];
for (NSString *aDateStringKey in sortedKeys)
{
    [valuesSorted addObject:countDict[aDateStringKey]];
}
NSLog(@"valuesSorted: %@", valuesSorted);

What's the logic:
Retrieve all the keys.
Sort the keys.
Iterate through that sorted key and append the corresponding value.

In your particular case, the NSString dates are comparables easily, and you could write instead :

NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compare:)];

Instead of :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH"];
NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull dateStr1, NSString * _Nonnull dateStr2) {
    NSDate *date1 = [dateFormatter dateFromString:dateStr1];
    NSDate *date2 = [dateFormatter dateFromString:dateStr2];
    return [date1 compare:date2];
}];

But that's a particular design because you have they have a dateFormat that allows it. Not all dateFormat allow it, so I wouldn't recommend to only base your thinking on that.

Upvotes: 1

Developer1205
Developer1205

Reputation: 167

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name"
    ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sortDescriptors];

Upvotes: -1

Related Questions