0

I am getting some post from Facebook for my application, I have all post data into a dictionary and that dictionary is in array. Now I have the post time from the Facebook which is in dictionary as string. I want to sort that array according to latest post which is known by the post time. I had seen several answers on SO but they did in a single dictionary I want to do that from different dictionary. enter image description here

This is my array of dictionary from which I have to sort array according to dictionary postTime value.

Code:

var dateFormatter: NSDateFormatter = NSDateFormatter()
                    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
                    var tempArray: NSMutableArray = NSMutableArray()
                    for var i = 0; i < dicts.count; i++ {
                        var dic: NSMutableDictionary = dicts[i] as! NSMutableDictionary
                        let dateConverted: NSDate = dateFormatter.dateFromString(dic["postTime"] as! String)!
                        dic["postTime"] = dateConverted
                        tempArray.addObject(dic)
                    }
                    var descriptor: NSSortDescriptor = NSSortDescriptor(key: "postTime", ascending: true)
                    var descriptors: NSArray = [descriptor]
                    var sortedArray: NSArray = tempArray.sortedArrayUsingDescriptors(descriptors as! [NSSortDescriptor])
                    NSLog("%@", sortedArray)

dicts is my array of dictionary and i have to take the post time which is seen in the image.

shahin ali agharia
  • 1,629
  • 4
  • 21
  • 36

2 Answers2

0
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"'Time: 'yyyy-MMM-dd HH:mm:ss";


    NSMutableArray *tempArray=[NSMutableArray new];
    for(NSInteger i=0;i<yourArrayOfDic.count ;i++){
        NSMutableDictionary *dic =[[NSMutableDictionary alloc] initWithDictionary:[yourArrayOfDic objectAtIndex:i]];
        NSDate *dateConverted = [dateFormatter dateFromString:[dic valueForKey:@"time"]];
        [dic setValue:dateConverted forKey:@"time"];
        [tempArray addObject:dic];
    }


    NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"time" ascending:YES];
    NSArray *descriptors=[NSArray arrayWithObject: descriptor];
    NSArray *sortedArray=[tempArray sortedArrayUsingDescriptors:descriptors];
    NSLog(@"%@",sortedArray);

// In Swift.. i m weak in swift but objective c to swift may help u . let me know it work or not !!!!!

var dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "'Time: 'yyyy-MMM-dd HH:mm:ss"
var tempArray: NSMutableArray = NSMutableArray()
for var i = 0; i < yourArrayOfDic.count; i++ {
    var dic: NSMutableDictionary = yourArrayOfDic[i]
    var dateConverted: NSDate = dateFormatter.dateFromString(dic["time"])
    dic["time"] = dateConverted
    tempArray.addObject(dic)
}
var descriptor: NSSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
var descriptors: NSArray = [descriptor]
var sortedArray: NSArray = tempArray.sortedArrayUsingDescriptors(descriptors)
NSLog("%@", sortedArray)
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
0

IOS Swift 3.2

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
//   dateFormatter.locale = Locale.current
let tempArray: NSMutableArray = NSMutableArray()
for i in 0 ..< self.yourarr.count{
    var dicdata  = self.yourarr[i] as! [String : Any]

    var dateConverted = dateFormatter.date(from: dicdata["time"] as! String)
    dicdata["time"] = dateConverted
                    tempArray.add(dicdata)
 }
 var descriptor: NSSortDescriptor = NSSortDescriptor(key: "time", ascending: true)
 var descriptors: NSArray = [descriptor]
 var sortedArray: NSArray = tempArray.sortedArray(using: descriptors as! [NSSortDescriptor]) as NSArray
 NSLog("%@", sortedArray)
Maulik Patel
  • 2,045
  • 17
  • 24