-4

I am working on Chat kind of application. Where I am fetching full message list from server and saving it into array of custom entity NSObject class.

Here is my class.h :

import

@interface ChatMessage : NSObject

@property (nonatomic, retain) NSString *msg_text;
@property (nonatomic, retain) NSDate *msg_date;
@property (nonatomic, assign) int sender_id;
@property (nonatomic, assign) int receiver_id;
@property (nonatomic, retain) NSString *msg_status;

@end

Here is my class.m :

#import "ChatMessage.h"

@implementation ChatMessage

@synthesize msg_text, msg_date, msg_status, sender_id, receiver_id;

- (id) initWithCoder: (NSCoder *)coder
{
    self = [[ChatMessage alloc] init];
    if (self != nil)
    {
        self.msg_text = [coder decodeObjectForKey:@"msg_text"];
        self.msg_date = [coder decodeObjectForKey:@"msg_date"];
        self.msg_status = [coder decodeObjectForKey:@"msg_status"];
        self.sender_id = [coder decodeIntForKey:@"sender_id"];
        self.receiver_id = [coder decodeIntForKey:@"receiver_id"];
    }
    return self;
}

- (void)encodeWithCoder: (NSCoder *)coder
{
    [coder encodeObject:msg_text forKey:@"msg_text"];
    [coder encodeObject:msg_date forKey:@"msg_date"];
    [coder encodeObject:msg_status forKey:@"msg_status"];
    [coder encodeInt:sender_id forKey:@"sender_id"];
    [coder encodeInt:receiver_id forKey:@"receiver_id"];
}

@end

I want to do sorting of an object of NSMutableArray with using "msg_date" property in my CellForRowAtIndexPath before displaying it into my chat list table view. I am getting date and time format as "2014-08-21 18:30:00" for each and every chat messages.

here is my code for sorting array of objects :

NSMutableArray *unsortedArray = [[NSMutableArray alloc] init];

//sorting array with date and time
for (int i=0; i<[self.arrayChatMessages count]; i++) {

ChatMessage *chat = [[ChatMessage alloc] init];
chat = [self.arrayChatMessages objectAtIndex:i];                          

[unsortedArray addObject:chat.msg_date];
}

NSLog(@"un sorted array is = %@", unsortedArray);

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
                                     return [obj2 compare:obj1];
                               }];

NSLog(@"sorted array is = %@", sortedArray);

It's sorting successfully. In my console i am getting :

un sorted array is = (
    "2014-08-21 18:28:58",
    "2014-08-21 18:27:41",
    "2014-08-21 20:10:45",
    "2014-08-21 18:30:45",
    "2014-08-29 12:27:45"
)

sorted array is = (
    "2014-08-29 12:27:45",
    "2014-08-21 20:10:45",
    "2014-08-21 18:30:45",
    "2014-08-21 18:28:58",
    "2014-08-21 18:27:41"
)

My problem is according to this sorted array, how can i sort array of objects i.e. self.arrayChatMessages.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anand Gautam
  • 2,541
  • 3
  • 34
  • 70
  • 1
    http://stackoverflow.com/questions/1132806/sort-nsarray-of-date-strings-or-objects Check this if it helps, don't forget to upvote the answer provided there if it works – channi Aug 29 '14 at 07:19
  • Thanks @channiI have checked this link but in my case instead of array of date and time, i have array of custom objects. My date & time is single string value, that i am getting in CellForRowAtindexPath table methods.. – Anand Gautam Aug 29 '14 at 07:23

1 Answers1

1

To sort an NSArray of custom objects, you can use -sortedArrayUsingComparator: method and rely on result of the -compare: method of NSString:

self.arrayChatMessages = [self.arrayChatMessages sortedArrayUsingComparator:^(ChatMessage *obj1, ChatMessage *obj2) {
    return [obj1.msg_date compare:obj2.msg_date];
}];

Also I would recommend converting your date property from NSString to NSDate using NSDateFormatter.

Misha Karpenko
  • 2,168
  • 17
  • 18
  • Still, with the strings and date format like `2014-01-01 00:00:00` it should work as expected. – Misha Karpenko Aug 29 '14 at 07:27
  • Thanks for giving me time.. let me apply in my code, i ll get back to you surely. – Anand Gautam Aug 29 '14 at 07:42
  • @AnandGautam I see. You make an NSArray of NSStrings. If you want to sort it this way, replace the return expression with this one: `return [obj1 compare:obj2];` – Misha Karpenko Aug 29 '14 at 08:24
  • Again i have updated my question, now sorting is working fine. But my problem is how to sort array of custom object using sortedArray. – Anand Gautam Aug 29 '14 at 08:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60216/discussion-between-anand-gautam-and-misha-karpenko). – Anand Gautam Aug 29 '14 at 08:29