ios - Tricky Object Sorting and Removing from NSMutableArray -


i have array of objects (that contain message), every object has same structure. has message property nsdictionary. 1 dictionary looks this:

<message: {     keycreatedate = "06/08/14 21:23";     keymessage = lorem ipsum;     keyreceiverchannel = samplestring;     keysenderuser = samplename; },... 

my goal make "inbox" display newest messages every user in each cell. want show newest messages each user, fb messages, what'sapp or imessage inbox, every cell in table view represents recent message friend. looks easy, it's harder imaged. need remove every message every friend, keep newest one. can remove message 1 user, can't keep newest while removing others. possible this?

i can remove message 1 specified user code:

    nsmutablearray *originalarray = [[nsmutablearray alloc]initwitharray:message];     nsmutablearray *objectstoremove = [[nsmutablearray alloc]init];     nsmutablearray *clonearray = [nsmutablearray arraywitharray:originalarray];      (nmessage *messageobject in originalarray) {         if ([messageobject.message[@"keysenderuser"] isequal:usernamestring]) {             [objectstoremove addobject:messageobject];         }     }      if ([objectstoremove count]>0) {         [clonearray removeobjectsinarray:objectstoremove];         nslog(@"deleted: %@", objectstoremove);         self.messagesarray = [[nsmutablearray alloc]initwitharray:clonearray]; 

i think easier, if somehow add exception [clonearray removeobjectsinarray:objectstoremove]; line, doesn't let remove newest ones. it's problem need sort messages based on keysenderuser before delete them. actual code can't that, can find 1 pre-defined user's message , remove of them. please share me if have idea. in advance.

i feel bottom better in case build new array contains latest message dictionary.

the solution below. 1. sort array using date. 2. create new list holding user messages. 2. loop through each object check whether user present, if not add created list.

the code solution below, pass in array of messaged, give list of messages latest user.

-(nsarray *) getlatestmessagelistforeveryuser:(nsarray *) array {     nsdateformatter *formatter = [[nsdateformatter alloc] init];     nsarray *sortedarray = [array sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2) {         nsdate *obj1date = [formatter datefromstring: [(nsdictionary *)obj1 objectforkey:@"keycreatedate" ] ];         nsdate *obj2date = [formatter datefromstring:[(nsdictionary *)obj2 objectforkey:@"keycreatedate" ] ];         return  ([obj1date earlierdate:obj2date] == obj1date);     }];       nsmutablearray *messagearray = [nsmutablearray array];     nsmutablearray *usersarray = [nsmutablearray array];      (nsdictionary *messagedictionary in sortedarray)     {         nsstring *username = [messagedictionary objectforkey:@"keysenderuser"];         if (![usersarray containsobject:username])         {             [usersarray addobject:username];             [messagearray addobject:messagedictionary];         }     }     return messagearray; } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -