Swift:按字母顺序排列对象的数组
我有这个:
class Movies { Name:String Date:Int }
和一组[电影]。 如何按名称按字母顺序排列数组? 我试过了:
movieArr = movieArr.sorted{ $0 < $1 }
和
movieArr = sorted(movieArr)
但这不起作用,因为我没有访问电影的名称属性。
在closures时,你要通过sort
,比较你想sorting的属性。 喜欢这个:
movieArr.sorted { $0.name < $1.name }
旁注:通常只有types以大写字母开头; 我build议使用name
和date
,而不是Name
和Date
。
例如,在操场上:
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]
使用Swift 3,您可以select以下方法之一来解决您的问题。
1.使用与不符合Comparable
协议的Movie
类一起使用sorted(by:)
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.使用符合Comparable
协议的Movie
类来sorted(by:)
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.使用与符合Comparable
协议的Movie
类一起使用sorted()
通过使您的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] */
对于那些使用Swift 3的人来说,接受答案的等价方法是:
movieArr.sorted { $0.Name < $1.Name }