如何在swift中根据时间对一组字典进行排序

时间:2016-11-02 08:21:23

标签: ios swift sorting date

我正在尝试根据具有日期的密钥对字典数组进行排序。因此,排序实际上必须在日期的基础上完成。但我不知道为什么sortedArrayUsingComparator没有对数组进行排序,而是我将旧数组放回原处。尽管如此,这些值在对象(obj1,obj2)中都是正确的。

这是过滤器数组:

(
        {
        age = 26;
        "already_reported" = no;
        "already_sync" = no;
        "complete_name" = "Ghazalahhhhhhhhhhhhh Khan";
        distance = "7.92353";
        "first_name" = Ghazalahhhhhhhhhhhhh;
        id = 288;
        "last_chat_details" =         (
                        {
                "from_xmpp_id" = ghazalah41e21475158541;
                id = 9614;
                message = H;
                "message_est_timing" = "2016-11-02 08:30:09";
                timestamp = 1478075409813132;
                "to_xmpp_id" = annungdg1475217973;
                "xmpp_message_id" = "F67A8495-E700-485B-A0B3-49C854E4FA45";
            }
        );
        "last_name" = Khan;
        "last_played_music" =         (
                        {
                album = "Haan Kargi (Jatt.fm)";
                artist = "Ammy Virk (Mr-Jatt.com)";
                bpm = 0;
                "created_at" = "0000-00-00 00:00:00";
                duration = "3.55";
                genre = "Single Tracks (Mr-Jatt.com)";
                id = 69251;
                "is_recently_played" = 1;
                "persistent_id" = 7705218523513871878;
                title = "Haan Kargi (Mr-Jatt.com)";
                "updated_at" = "0000-00-00 00:00:00";
                "user_id" = 288;
            }
        );
        "matched_profile_image" =         (
                        {
                "est_date_time" = "2016-10-04 04:08:34";
                id = 479;
                "image_order" = 3;
                "is_profile_image" = no;
                "main_image_id" = 2225;
                "main_image_path" = "/files/profile_images/934820161004080721.jpg";
                "thumb_image_path" = "/files/profile_images/t_934820161004080721.jpg";
                "user_id" = 288;
            },
                        {
                "est_date_time" = "2016-10-04 04:08:34";
                id = 480;
                "image_order" = 2;
                "is_profile_image" = no;
                "main_image_id" = 2226;
                "main_image_path" = "/files/profile_images/685220161004080800.jpg";
                "thumb_image_path" = "/files/profile_images/t_685220161004080800.jpg";
                "user_id" = 288;
            },
                        {
                "est_date_time" = "2016-10-28 06:08:37";
                id = 552;
                "image_order" = 1;
                "is_profile_image" = yes;
                "main_image_id" = 2313;
                "main_image_path" = "/files/profile_images/825720161028100832.jpg";
                "thumb_image_path" = "/files/profile_images/t_825720161028100832.jpg";
                "user_id" = 288;
            }
        );
        "short_description" = Church;
        username = ghazalah41e21475158541;
    },
        {
        age = 26;
        "already_reported" = no;
        "already_sync" = no;
        "complete_name" = "Michael Berrin";
        distance = "7.92490";
        "first_name" = Michael;
        id = 310;
        "last_chat_details" =         (
                        {
                "from_xmpp_id" = michaelgbmh1477565051;
                id = 9143;
                message = Hi;
                "message_est_timing" = "2016-11-01 07:43:46";
                timestamp = 1477986226750744;
                "to_xmpp_id" = annungdg1475217973;
                "xmpp_message_id" = "CD8FD64D-1370-41AD-8731-BC2A4C16EB6D";
            }
        );
        "last_name" = Berrin;
        "last_played_music" =         (
                        {
                album = "T-Series";
                artist = "";
                bpm = 0;
                "created_at" = "0000-00-00 00:00:00";
                duration = "5.04";
                genre = "";
                id = 72284;
                "is_recently_played" = 1;
                "persistent_id" = 4446247237416765338;
                title = "Dheere Dheere Se Meri Zindagi Video Song (OFFICIAL) Hrithik Roshan, Sonam Kapoor | Yo Yo Honey Singh - YouTube";
                "updated_at" = "0000-00-00 00:00:00";
                "user_id" = 310;
            }
        );
        "matched_profile_image" =         (
                        {
                "est_date_time" = "2016-10-27 07:04:10";
                id = 543;
                "image_order" = 4;
                "is_profile_image" = no;
                "main_image_id" = 2303;
                "main_image_path" = "/files/profile_images/186120161027110402.jpg";
                "thumb_image_path" = "/files/profile_images/t_186120161027110402.jpg";
                "user_id" = 310;
            },
                        {
                "est_date_time" = "2016-10-27 07:12:52";
                id = 544;
                "image_order" = 3;
                "is_profile_image" = no;
                "main_image_id" = 2304;
                "main_image_path" = "/files/profile_images/132120161027111225.jpg";
                "thumb_image_path" = "/files/profile_images/t_132120161027111225.jpg";
                "user_id" = 310;
            },
                        {
                "est_date_time" = "2016-10-27 07:12:52";
                id = 545;
                "image_order" = 1;
                "is_profile_image" = yes;
                "main_image_id" = 2305;
                "main_image_path" = "/files/profile_images/270620161027111236.jpg";
                "thumb_image_path" = "/files/profile_images/t_270620161027111236.jpg";
                "user_id" = 310;
            },
                        {
                "est_date_time" = "2016-10-27 07:12:52";
                id = 546;
                "image_order" = 2;
                "is_profile_image" = no;
                "main_image_id" = 2306;
                "main_image_path" = "/files/profile_images/920120161027111247.jpg";
                "thumb_image_path" = "/files/profile_images/t_920120161027111247.jpg";
                "user_id" = 310;
            }
        );
        "short_description" = "";
        username = michaelgbmh1477565051;
    },
        {
        age = 22;
        "already_reported" = no;
        "already_sync" = no;
        "complete_name" = "Lisa Benson";
        distance = "7.92195";
        "first_name" = Lisa;
        id = 287;
        "last_chat_details" =         {
        };
        "last_name" = Benson;
        "last_played_music" =         (
                        {
                album = "";
                artist = "";
                bpm = 125;
                "created_at" = "0000-00-00 00:00:00";
                duration = "3.1";
                genre = "";
                id = 76890;
                "is_recently_played" = 1;
                "persistent_id" = 577117298;
                title = "Avicii -";
                "updated_at" = "0000-00-00 00:00:00";
                "user_id" = 287;
            }
        );
        "matched_profile_image" =         (
                        {
                "est_date_time" = "2016-09-29 08:54:10";
                id = 459;
                "image_order" = 2;
                "is_profile_image" = no;
                "main_image_id" = 2200;
                "main_image_path" = "/files/profile_images/858820160929125409.jpg";
                "thumb_image_path" = "/files/profile_images/t_858820160929125409.jpg";
                "user_id" = 287;
            },
                        {
                "est_date_time" = "2016-10-04 06:58:35";
                id = 481;
                "image_order" = 1;
                "is_profile_image" = no;
                "main_image_id" = 2227;
                "main_image_path" = "/files/profile_images/292820161004105734.jpg";
                "thumb_image_path" = "/files/profile_images/t_292820161004105734.jpg";
                "user_id" = 287;
            }
        );
        "short_description" = "";
        username = lisavmds1475153650;
    }
)

这是我的代码,请帮助:

let tempArr: NSArray! = self.filteredArray.sortedArrayUsingComparator({
            (obj1:AnyObject!, obj2:AnyObject!) -> NSComparisonResult in
            let dateStr1 = (obj1["last_chat_details"] as! NSArray).firstObject!["message_est_timing"] as! String
            let date1: NSDate = dateFormatter.dateFromString(dateStr1 as String)!
            let dateStr2 = (obj2["last_chat_details"] as! NSArray).firstObject!["message_est_timing"] as! NSString
            let date2: NSDate = dateFormatter.dateFromString(dateStr2 as String)!

            return date2.compare(date1)

        }
    )

    print("the temp msgs are\(tempArr)")
enter code here

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

let array = [
    ["message_est_timing" : Date(timeIntervalSince1970: 100)] ,
    ["message_est_timing" : Date(timeIntervalSince1970: 300)] ,
    ["message_est_timing" : Date(timeIntervalSince1970: 400)]
]
let newArr = array.sorted { (dict1, dict2) -> Bool in
    let date1 = dict1["message_est_timing"]!
    let date2 = dict2["message_est_timing"]!
    return date1.compare(date2).rawValue == 1 ? true : false
}

newArr将按日期正确排序。