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.

6
  • stackoverflow.com/questions/9529620/… Commented Feb 25, 2016 at 7:28
  • check this stackoverflow.com/questions/6083408/… Commented Feb 25, 2016 at 7:28
  • do you want to sort dictionary? dictionary is unsorted collection. in reality you would like to sort an array. Commented Feb 25, 2016 at 8:05
  • I have to sort array but from the dictionary values Commented Feb 25, 2016 at 8:18
  • have u tried given answer ????? Commented Feb 25, 2016 at 8:59

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

On this line let dateConverted: NSDate = dateFormatter.dateFromString(dic["postTime"]) it shows an error of Cannot convert value of type 'AnyObject?' to expected argument type 'String'.
var dateConverted: NSDate = dateFormatter.dateFromString(dic["time"] as! String)
Now its getting crashed showing unexpectedly found nil while unwrapping an Optional value.
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)

Comments

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.