ios Swift 2.0 按属性对对象数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31729337/
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 2.0 Sorting Array of Objects by Property
提问by felix_xiao
In Swift 2.0, how would you go about sorting an array of custom objects by a property? I know in Swift 1.2, this was done using sorted() and sort(). However, these methods no longer work in Xcode 7 beta 4. Thanks!
在 Swift 2.0 中,您将如何按属性对自定义对象数组进行排序?我知道在 Swift 1.2 中,这是使用 sorted() 和 sort() 完成的。但是,这些方法在 Xcode 7 beta 4 中不再有效。谢谢!
For example:
例如:
class MyObject: NSObject {
var myDate : NSDate
}
...
let myObject1 : MyObject = MyObject() //same thing for myObject2, myObject3
var myArray : [MyObject] = [myObject1, myObject2, myObject3]
//now, I want to sort myArray by the myDate property of MyObject.
回答by Rob
In Swift 2:
在 Swift 2 中:
You can use
sort
method, usingcompare
to compare the two dates:let sortedArray = myArray.sort {
.myDate.compare(.myDate) == .OrderedAscending } // use `sorted` in Swift 1.2myArray.sortInPlace {
.myDate.compare(.myDate) == .OrderedAscending } // use `sort` in Swift 1.2let sortedArray = myArray.sort {
.myDate.compare(.myDate) == .OrderedAscending } // use `sorted` in Swift 1.2myArray.sortInPlace {
.myDate.compare(.myDate) == .OrderedAscending } // use `sort` in Swift 1.2let sortedArray = myArray.sorted {
.myDate < .myDate }myArray.sort {
.myDate < .myDate }let sortedArray = myArray.sorted {
.myDate < .myDate }myArray.sort {
.myDate < .myDate }var myCustomerArray = [Customer]() myCustomerArray.sortInPlace {(customer1:Customer, customer2:Customer) -> Bool in customer1.id < customer2.id }
Or, if you want to sort the original array, you can
sortInPlace
:myArray.sort(by: { (one: MyObject, two: MyObject) -> Bool in one. myDate < two. myDate })
您可以使用
##代码##sort
方法,compare
用于比较两个日期:或者,如果您想对原始数组进行排序,您可以
##代码##sortInPlace
:
In Swift 3:
在 Swift 3 中:
to return a sorted rendition of the array, use
##代码##sorted
, notsort
to sort in place, it's now just
##代码##sort
:
要返回数组的排序再现,请使用
##代码##sorted
,而不是sort
排序到位,现在只是
##代码##sort
:
And with Swift 3's Date
type, you can use the <
operator.
使用 Swift 3 的Date
类型,您可以使用<
运算符。
回答by Hanny
If you want to sort original array of custom objects. Here is another way to do so in Swift 2.1
如果要对自定义对象的原始数组进行排序。这是Swift 2.1 中的另一种方法
##代码##Where id
is an Integer. You can use the same <
operator for String
as well.
id
整数在哪里。您也可以使用相同的<
运算符String
。
You can learn more about its use by looking at an example here: Swift2: Nearby Customers
您可以通过查看此处的示例了解有关其使用的更多信息: Swift2:附近的客户
回答by jaiswal Rajan
In swift 3.0
在快速 3.0
I have taken the assumption same as the example asked in the question.
我采取了与问题中提出的示例相同的假设。
i.e customObject is MyObject with variable myDate. The array of the MyObject is myArray.We can sort like this simply.
即 customObject 是带有变量 myDate 的 MyObject。MyObject 的数组是 myArray。我们可以这样简单地排序。
##代码##回答by gnasher729
sort is now sortInPlace (or something similar) with the same parameters as sort.
sort 现在是 sortInPlace(或类似的东西),具有与 sort 相同的参数。