如何在Swift中对数组数组进行排序?

时间:2015-11-20 04:37:47

标签: arrays swift sorting parse-platform

我从Parse回来了一组数组。它们被命名为vid1到vid10。我正在尝试对它们进行排序,因此当我将各个元素放在相应的自定义数组中时,它们将按顺序排列。请帮我1.在哪里实施和2.如何?这是我的代码......

let query = PFQuery(className: "UserVideos")
        query.whereKey("userObjectId", equalTo: PFUser.currentUser()!.objectId!)
        query.findObjectsInBackgroundWithBlock { (vid:Array?, error:NSError?) -> Void in
            if vid != nil
            {
                for items in vid!
                {
                    if !(items["vid1"] == nil)
                    {
                        if let myfav1 = items["vid1"] as? NSArray
                        {
                            let id = myfav1[0] as! String
                            let rank = myfav1[1] as! String
                            let title = myfav1[2] as! String
                            let description = myfav1[3] as! String
                            let image = myfav1[4] as! String
                            self.vidRankArray.append(rank)
                            self.vidIdArray.append(id)
                            self.vidTitleArray.append(title)
                            self.vidDescriptionArray.append(description)
                            self.vidImageArray.append(image)
                        }
                    }
                    if !(items["vid2"] == nil)
                    {
                        if let myfav2 = items["vid2"] as? NSArray
                        {
                            let id = myfav2[0] as! String
                            let rank = myfav2[1] as! String
                            let title = myfav2[2] as! String
                            let description = myfav2[3] as! String
                            let image = myfav2[4] as! String
                            self.vidRankArray.append(rank)
                            self.vidIdArray.append(id)
                            self.vidTitleArray.append(title)
                            self.vidDescriptionArray.append(description)
                            self.vidImageArray.append(image)
                        }
                    }

***** UPDATE ******

这就是我从Parse回来的视频!这就是我要排序的内容

[<UserVideos: 0x7fa068f15430, objectId: hbqSBoHsxa, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid1 =     (
        "yWfsla_Uh80",
        1,
        "EXO_CALL ME BABY_Music Video",
        "Download on iTunes : KOR https://itunes.apple.com/album/the-2nd-album-exodus/id979878364 CHN ...",
        "https://i.ytimg.com/vi/yWfsla_Uh80/default.jpg"
    );
}, <UserVideos: 0x7fa068f171e0, objectId: 7S9oT6DePb, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid2 =     (
        "dF3fc0K_7Nw",
        2,
        "blackqueen's 2ne1 kpop mix cover dance",
        "On the day of the foreign embassies in Korea.. blackqueen's 2ne1 kpop mix Invitation performance \Uc8fc\Ud55c\Uc678\Uad6d\Ub300\Uc0ac\Uad00\Uc758 \Ub0a0 \Ube14\Ub799\Ud038\Uc758 2ne1 \Ubbf9\Uc2a4 \Uacf5\Uc5f0.",
        "https://i.ytimg.com/vi/dF3fc0K_7Nw/default.jpg"
    );
}, <UserVideos: 0x7fa068f17a90, objectId: MHZOmKu3Qw, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid3 =     (
        MBNQgq56egk,
        3,
        "BIGBANG - \Ub9e8\Uc815\Uc2e0(SOBER) M/V",
        "Available on iTunes @ http://smarturl.it/BIGBANGMADE_D Available on Spotify @ http://sptfy.com/BIGBANGMADE_D #  BIGBANG #  \Ube45\Ubc45 #SOBER #\Ub9e8\Uc815\Uc2e0 ...",
        "https://i.ytimg.com/vi/MBNQgq56egk/default.jpg"
    );
}, <UserVideos: 0x7fa068f18240, objectId: e5TiEbjdhr, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid7 =     (
        PfUlE2LlGro,
        7,
        "TVXQ! \Ub3d9\Ubc29\Uc2e0\Uae30_Catch Me_Music Video",
        "TVXQ! new album 'Catch Me' to be released on September 24th at noon (Seoul Time) through various music websites! \U266a Download on iTunes ...",
        "https://i.ytimg.com/vi/PfUlE2LlGro/default.jpg"
    );
}, <UserVideos: 0x7fa068f189f0, objectId: UKvjXI5TXh, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid10 =     (
        PfUlE2LlGro,
        10,
        "TVXQ! \Ub3d9\Ubc29\Uc2e0\Uae30_Catch Me_Music Video",
        "TVXQ! new album 'Catch Me' to be released on September 24th at noon (Seoul Time) through various music websites! \U266a Download on iTunes ...",
        "https://i.ytimg.com/vi/PfUlE2LlGro/default.jpg"
    );
}, <UserVideos: 0x7fa068f19200, objectId: zCQTXde30Q, localId: (null)> {
    userObjectId = 4k5eUtDPnz;
    vid8 =     (
        gAal8xHfV0c,
        8,
        "EXO_\Ub291\Ub300\Uc640 \Ubbf8\Ub140 (Wolf)_Music Video (Korean ver.)",
        "Download on iTunes : https://itunes.apple.com/us/album/the-1st-album-xoxo/id651337773 \U266a For more information \U261e smtown hompage ...",
        "https://i.ytimg.com/vi/gAal8xHfV0c/default.jpg"
    );
}]

1 个答案:

答案 0 :(得分:1)

您将很难保持所有这些阵列同步。我会创建一个Video类,其属性为videoID,rank,title,description和image。然后根据你想要的任何属性排序。如果它排名,它看起来像这样:

let sortedVideos = videos.sort { $0.rank < $1.rank }

如果由于某种原因你需要拉出属性的数组,你可以使用map:

let titles = videos.map { $0.title }