我有这个:
class Movies {
Name:String
Date:Int
}
和一组[电影]。如何按名称按字母顺序对数组进行排序?我试过了:
movieArr = movieArr.sorted{ $0 < $1 }
和
movieArr = sorted(movieArr)
但这不起作用,因为我没有访问电影的名称属性。
答案 0 :(得分:233)
在传递给sort
的闭包中,比较要排序的属性。像这样:
movieArr.sorted { $0.name < $1.name }
旁注:通常只有类型以大写字母开头;我建议使用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]
答案 1 :(得分:38)
使用Swift 3,您可以选择以下方法之一来解决问题。
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]
*/
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]
*/
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()