Swift:按字母顺序排列对象数组

时间:2014-11-03 17:36:55

标签: ios arrays swift sorting

我有这个:

class Movies {
  Name:String
  Date:Int
}

和一组[电影]。如何按名称按字母顺序对数组进行排序?我试过了:

movieArr = movieArr.sorted{ $0 < $1 }

movieArr = sorted(movieArr)

但这不起作用,因为我没有访问电影的名称属性。

7 个答案:

答案 0 :(得分:233)

在传递给sort的闭包中,比较要排序的属性。像这样:

movieArr.sorted { $0.name < $1.name }

旁注:通常只有类型以大写字母开头;我建议使用namedate,而不是NameDate


示例,在操场上:

class Movie {
    let name: String
    var date: Int?

    init(_ name: String) {
        self.name = name
    }
}

var movieA = Movie("A")
var movieB = Movie("B")
var movieC = Movie("C")

let movies = [movieB, movieC, movieA]
let sortedMovies = movies.sorted { $0.name < $1.name }
sortedMovies

sortedMovies将按照[movieA, movieB, movieC]

的顺序排列

答案 1 :(得分:38)

使用Swift 3,您可以选择以下方法之一来解决问题。

1。将sorted(by:​)与不符合Movie协议的Comparable类一起使用

如果您的Movie类不符合Comparable协议,则必须在闭包中指定您希望使用Array sorted(by:​)方法的属性。

Movie班级声明:

import Foundation

class Movie: CustomStringConvertible {

    let name: String
    var date: Date
    var description: String { return name }

    init(name: String, date: Date = Date()) {
        self.name = name
        self.date = date
    }

}

用法:

let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")

let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted(by: { $0.name < $1.name })
// let sortedMovies = movies.sorted { $0.name < $1.name } // also works

print(sortedMovies)

/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/

2。将sorted(by:​)与符合Movie协议

Comparable类一起使用

但是,通过使Movie类符合Comparable协议,当您想要使用Array的sorted(by:​)方法时,可以使用简洁的代码。

Movie班级声明:

import Foundation

class Movie: CustomStringConvertible, Comparable {

    let name: String
    var date: Date
    var description: String { return name }

    init(name: String, date: Date = Date()) {
        self.name = name
        self.date = date
    }

    static func ==(lhs: Movie, rhs: Movie) -> Bool {
        return lhs.name == rhs.name
    }

    static func <(lhs: Movie, rhs: Movie) -> Bool {
        return lhs.name < rhs.name
    }

}

用法:

let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")

let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted(by: { $0 < $1 })
// let sortedMovies = movies.sorted { $0 < $1 } // also works
// let sortedMovies = movies.sorted(by: <) // also works

print(sortedMovies)

/*
 prints: [Avatar, Piranha II: The Spawning, Titanic]
 */

3。将sorted()与符合Movie协议

Comparable类一起使用

通过使Movie类符合Comparable协议,您可以使用Array的sorted()方法替代sorted(by:​)

Movie班级声明:

import Foundation

class Movie: CustomStringConvertible, Comparable {

    let name: String
    var date: Date
    var description: String { return name }

    init(name: String, date: Date = Date()) {
        self.name = name
        self.date = date
    }

    static func ==(lhs: Movie, rhs: Movie) -> Bool {
        return lhs.name == rhs.name
    }

    static func <(lhs: Movie, rhs: Movie) -> Bool {
        return lhs.name < rhs.name
    }

}

用法:

let avatarMovie = Movie(name: "Avatar")
let titanicMovie = Movie(name: "Titanic")
let piranhaMovie = Movie(name: "Piranha II: The Spawning")

let movies = [avatarMovie, titanicMovie, piranhaMovie]
let sortedMovies = movies.sorted()

print(sortedMovies)

/*
 prints: [Avatar, Piranha II: The Spawning, Titanic]
 */

答案 2 :(得分:13)

对于那些使用Swift 3的人来说,接受答案的等效方法是:

movieArr.sorted { $0.Name < $1.Name }

答案 3 :(得分:13)

let sortArray =  array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })

答案 4 :(得分:0)

排序数组 雨燕4.2

arrayOfRaces = arrayOfItems.sorted(by: { ($0["raceName"] as! String) < ($1["raceName"] as! String) })

答案 5 :(得分:0)

*import Foundation
import CoreData


extension Messages {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Messages> {
        return NSFetchRequest<Messages>(entityName: "Messages")
    }

    @NSManaged public var text: String?
    @NSManaged public var date: Date?
    @NSManaged public var friends: Friends?

}

    //here arrMessage is the array you can sort this array as under bellow 

    var arrMessages = [Messages]()

    arrMessages.sort { (arrMessages1, arrMessages2) -> Bool in
               arrMessages1.date! > arrMessages2.date!
    }*

答案 6 :(得分:0)

由于无法使用基于语言环境的比较进行排序,因此大多数答案都是错误的。看看localizedStandardCompare()