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

我有这个:

class Movies {
Name:String
Date:Int
}

以及一个[ Movies ]数组。如何按名称的字母顺序对数组进行排序? 我试过:

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

还有

movieArr = sorted(movieArr)

但这不起作用,因为我没有访问 Movies 的 name 属性。

155155 次浏览

在传递给 sort的闭包中,比较要排序的属性,如下所示:

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

或下列情况下,你想绕过案件:

movieArr.sorted { $0.name.lowercased() < $1.name.lowercased() }

旁注: 通常只有类型以大写字母开头; 我建议使用 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]的顺序排列

Swift5更新

channelsArray = channelsArray.sorted { (channel1, channel2) -> Bool in
let channelName1 = channel1.name
let channelName2 = channel2.name
return (channelName1.localizedCaseInsensitiveCompare(channelName2) == .orderedAscending)
}

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


1. 使用 sorted(by:​)和不符合 Comparable协议的 Movie

如果您的 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:​)和符合 Comparable协议的 Movie

然而,通过使您的 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()和符合 Comparable协议的 Movie

通过使您的 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 }
let sortArray =  array.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })

排序数组 Swift 4.2

arrayOfRaces = arrayOfItems.sorted(by: { ($0["raceName"] as! String) < ($1["raceName"] as! String) })
*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!
}*

由于未能使用基于区域设置的比较进行排序,这些答案大多数都是错误的。查看 localizedStandardCompare ()