0

I have NSMutableArray like this:

// NSMutableArray *Data = (NSMUtableArray(NSDictionary))
       Data = {"Device": "A1", "Temperature": "25", "datetime": "2013.02.03"},
       {"Device": "A2", "Temperature": "25", "datetime": "2013.02.03"},         
       {"Device": "A3", "Temperature": "25", "datetime": "2013.02.03"},        
       {"Device": "A1", "Temperature": "25", "datetime": "2013.02.04"},
       {"Device": "A2", "Temperature": "25", "datetime": "2013.02.04"}.

How can I breakdown and group it into separated NSMutableArray follow "date time". As above case will be divide to:

Data_d1 = {"Device": "A1", "Temperature": "25", "datetime": "2013.02.03"},
          {"Device": "A2", "Temperature": "25", "datetime": "2013.02.03"},
          {"Device": "A3", "Temperature": "25", "datetime": "2013.02.03"}

And

Data_d2 = {"Device": "A1", "Temperature": "25", "datetime": "2013.02.04"},
          {"Device": "A2", "Temperature": "25", "datetime": "2013.02.04"}
5
  • On what basis do you want to break them down? Commented Jul 25, 2018 at 10:05
  • I think on basis of date "2013.02.04" and "2013.02.03" Commented Jul 25, 2018 at 10:05
  • @Jacky - What if there are more then 2 dates. Do you want to make n number of arrays based on n number of diff dates ? Commented Jul 25, 2018 at 10:09
  • @Rizwan yes I want to make it for "n" number of date (above data just a example) Commented Jul 25, 2018 at 15:13
  • @JackyLe - Alright, so the answer that I gave should work for "n" number of elements already. you may upvote if solution worked and you like it :). Commented Jul 25, 2018 at 15:17

3 Answers 3

1

if you have array of array of dictionary like this :

var data = [["Device": "A1", "Temperature": "25", "datetime": "2013.02.02"],
["Device": "A2", "Temperature": "25", "datetime": "2013.02.03"],
["Device": "A3", "Temperature": "25", "datetime": "2013.02.03"],
["Device": "A1", "Temperature": "25", "datetime": "2013.02.04"],
["Device": "A2", "Temperature": "25", "datetime": "2013.02.04"]]

Then you can segregate them on basis of any key as follows:

var dateArray = data.compactMap{ $0["datetime"] }
let uDateArray = Array(Set(dateArray))
var segregatedArray = [[[String: String]]]()
for dateTime in uDateArray {
    segregatedArray.append(data.filter{$0["datetime"] == dateTime})
}
print(segregatedArray);

Adding as per request Objective-C version with Array of dictionary as raw data.

Consider you have array of dictionary like following:

 NSArray *array=@[
     @{@"Device": @"A1",@"Temperature":@"25",@"datetime":@"2013.02.03"},
     @{@"Device":@"A2",@"Temperature":@"25",@"datetime":@"2013.02.03"},
     @{@"Device":@"A3",@"Temperature":@"25",@"datetime":@"2013.02.03"},
     @{@"Device":@"A1",@"Temperature":@"25",@"datetime":@"2013.02.04"},
     @{@"Device":@"X1",@"Temperature":@"26",@"datetime":@"2013.02.07"},
     @{@"Device":@"Y1",@"Temperature":@"27",@"datetime":@"2013.02.07"},
     @{@"Device":@"A2",@"Temperature":@"25",@"datetime":@"2013.02.04"}];

Then you can segregate them as follows:

    NSMutableDictionary* mutableDic = [[NSMutableDictionary alloc] initWithCapacity:1];
    for (NSDictionary* dic in array) {
        NSString* dateTime = [dic valueForKey:@"datetime"];
        NSMutableArray* data = [mutableDic valueForKey:dateTime];
        if(data != nil)//data with this key avalaibe, add further.
        {
            [data addObject:dic];
        }
        else//data with this key not avalaibe, make a mutable array with this dic and add.
        {
            NSMutableArray* newData = [[NSMutableArray alloc] initWithObjects:dic, nil];
            [mutableDic setValue:newData forKey:dateTime];
        }
    }
    NSLog(@"%@", mutableDic);

you can segregate on basis of any key just put that key here [dic valueForKey:@"datetime"];. Instead of datetime, you may put Device as asked in comment.

Screen shot of debugger after segregation :

enter image description here

Sign up to request clarification or add additional context in comments.

8 Comments

is code write by Objective-C. Seem it not. Could you change it to objective C
This code is in Swift. As you asked, I will also add Objective-C version for this. However I would suggest you to start learning / working on swift now :)
Yes, I'm learning on that. Start from Objective-C then will move to swift. Could you share code for objective-c
Yes, I will do it in sometime, once I am back on my MAC. What I will do is will iterate through each elements in the current NSMutableArray then on basis of dateTime will segregate it in a NSMutableDisctionary with the keys as dateTime. In this way diff dateTime keys will hold NSArray of that dateTime. This way basis of key in dictionary you will easily query for data of any particular date.
thanks. Actually my date time string is: "2018-07-04 16:09:22" and I want to group by date only "2018-07-04".
|
1

You can achieve it using the below code:

let data:[[String: String]] = [
        ["Device": "A1",
         "Temperature": "25",
         "datetime": "2013.02.03"],

        ["Device": "A2",
         "Temperature": "25",
         "datetime": "2013.02.03"],

        ["Device": "A3",
         "Temperature": "25",
         "datetime": "2013.02.03"],

        ["Device": "A1",
         "Temperature": "25",
         "datetime": "2013.02.04"],

        ["Device": "A2",
         "Temperature": "25",
         "datetime": "2013.02.04"]]

    let dateTimeArray = data.compactMap({ $0["datetime"] })
    let uniqueDateTimeArray = Array(Set(dateTimeArray))

    var desiredSeparatedArray = [[[String: String]]]()
    for dateTime in uniqueDateTimeArray {
        var arrayForDateTime = [[String: String]]()
        for element in data {
            if element["datetime"] == dateTime {
                arrayForItem.append(element)
            }
        }
        desiredSeparatedArray.append(arrayForDateTime)
    }

The final desiredSeparatedArray is the answer!!

2 Comments

Can you write it under objective-C
The objective C code will be similar only, i think if you understand the answer then you should be able to write it. Let me know if there is something you are not able to get.
0

try this

let array = Data as! [[String:String]]

let dictionary = Dictionary(grouping: array, by: {$0["datetime"]!})
var listOfArray = [[[String:String]]]()
dictionary.forEach({listOfArray.append($0.value)})

listOfArray will have all your arrays

1 Comment

Can you share how to do it in Objective C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.