我有这个:
class Movies {
Name:String
Date:Int
}
和[电影]数组。如何按名称的字母顺序对数组进行排序?我试过了:
movieArr = movieArr.sorted{ $0 < $1 }
和
movieArr = sorted(movieArr)
但这不起作用,因为我没有访问电影的名称属性。
我有这个:
class Movies {
Name:String
Date:Int
}
和[电影]数组。如何按名称的字母顺序对数组进行排序?我试过了:
movieArr = movieArr.sorted{ $0 < $1 }
和
movieArr = sorted(movieArr)
但这不起作用,因为我没有访问电影的名称属性。
Answers:
在传递给的闭包中sort
,比较要排序的属性。像这样:
movieArr.sorted { $0.name < $1.name }
或以下要绕过案例的案例:
movieArr.sorted { $0.name.lowercased() < $1.name.lowercased() }
旁注:通常,只有类型以大写字母开头;我建议使用name
and date
,而不是Name
and 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]
Swift5更新
channelsArray = channelsArray.sorted { (channel1, channel2) -> Bool in
let channelName1 = channel1.name
let channelName2 = channel2.name
return (channelName1.localizedCaseInsensitiveCompare(channelName2) == .orderedAscending)
.lowercaseString
将解决此问题。
sorted
再次在Swift 3中:D。
使用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]
*/
movies.sorted(by: <)
和除外movies.sorted()
)?
sorted(by:)
和sorted()
是两种不同的方法。您可以array.sorted()
用作的替代方法array.sorted(by: <)
。
sorted(by:)
和定义相同的类sorted()
吗?
Comparable
协议的类/结构具有相同的实现。
"1" "14""28""31""4""42""49"
let sortArray = array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
对于使用Swift 3的用户,可接受的答案的等效方法是:
movieArr.sorted { $0.Name < $1.Name }
*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!
}*