ios Swift:按字母顺序对对象数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26719744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: Sort array of objects alphabetically
提问by Jonah Ruffer
I have this:
我有这个:
class Movies {
Name:String
Date:Int
}
and an array of [Movies]. How do I sort the array alphabetically by name? I've tried:
和一系列[电影]。如何按名称的字母顺序对数组进行排序?我试过了:
movieArr = movieArr.sorted{ $0 < $1 }
movieArr = movieArr.sorted{ $0 < $1 }
and
和
movieArr = sorted(movieArr)
movieArr = sorted(movieArr)
but that doesn't work because I'm not accessing the name attribute of Movies.
但这不起作用,因为我没有访问 Movies 的 name 属性。
回答by Mike S
In the closure you pass to sort, compare the properties you want to sort by. Like this:
在传递给 的闭包中sort,比较要作为排序依据的属性。像这样:
movieArr.sorted { movieArr.sorted { 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 { 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
}
}
.name < .name }
sortedMovies
.name.lowercased() < .name.lowercased() }
.name < .name }
or the following in the cases that you want to bypass cases:
或在您想要绕过案例的情况下执行以下操作:
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: { 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
}
}
.name < .name })
// let sortedMovies = movies.sorted { 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: { 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 sortedMovies = movies.sorted { 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]
*/
< } // also works
// let sortedMovies = movies.sorted(by: <) // also works
print(sortedMovies)
/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/
.name < .name } // also works
print(sortedMovies)
/*
prints: [Avatar, Piranha II: The Spawning, Titanic]
*/
Sidenote:Typically only types start with an uppercase letter; I'd recommend using nameand date, not Nameand Date.
旁注:通常只有类型以大写字母开头;我建议使用nameand date,而不是Nameand Date。
Example, in a playground:
例如,在操场上:
let sortArray = array.sorted(by: { movieArr.sorted { arrayOfRaces = arrayOfItems.sorted(by: { (*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!
}*
["raceName"] as! String) < (["raceName"] as! String) })
.Name < .Name }
.name.lowercased() < .name.lowercased() })
sortedMovieswill be in the order [movieA, movieB, movieC]
sortedMovies将按顺序 [movieA, movieB, movieC]
回答by Imanou Petit
With Swift 3, you can choose one of the following ways to solve your problem.
使用 Swift 3,您可以选择以下方法之一来解决您的问题。
1. Using sorted(by:?)with a Movieclass that does not conform to Comparableprotocol
1.sorted(by:?)与Movie不符合Comparable协议的类一起使用
If your Movieclass does not conform to Comparableprotocol, you must specify in your closure the property on which you wish to use Array's sorted(by:?)method.
如果你的Movie类不符合Comparable协议,你必须在你的闭包中指定你希望使用 Arraysorted(by:?)方法的属性。
Movieclass declaration:
Movie类声明:
Usage:
用法:
##代码##2. Using sorted(by:?)with a Movieclass that conforms to Comparableprotocol
2.sorted(by:?)与Movie符合Comparable协议的类一起使用
However, by making your Movieclass conform to Comparableprotocol, you can have a much concise code when you want to use Array's sorted(by:?)method.
但是,通过使您的Movie类符合Comparable协议,当您想要使用 Array 的sorted(by:?)方法时,您可以获得更简洁的代码。
Movieclass declaration:
Movie类声明:
Usage:
用法:
##代码##3. Using sorted()with a Movieclass that conforms to Comparableprotocol
3.sorted()与Movie符合Comparable协议的类一起使用
By making your Movieclass conform to Comparableprotocol, you can use Array's sorted()method as an alternative to sorted(by:?).
通过使您的Movie类符合Comparable协议,您可以使用 Array 的sorted()方法作为sorted(by:?).
Movieclass declaration:
Movie类声明:
Usage:
用法:
##代码##回答by m.alqadi
回答by Jacobo Koenig
For those using Swift 3, the equivalent method for the accepted answer is:
对于那些使用 Swift 3 的人,接受答案的等效方法是:
##代码##回答by CSE 1994
Sorted array Swift 4.2
排序数组 Swift 4.2
##代码##
