Swift如何按属性值对自定义对象数组进行排序

本文翻译自:Swift how to sort array of custom objects by property value

lets say we have a custom class named imageFile and this class contains two properties. 可以说我们有一个名为imageFile的自定义类,该类包含两个属性。

class imageFile  {
    var fileName = String()
    var fileID = Int()
}

lots of them stored in Array 其中很多存储在数组中

var images : Array = []

var aImage = imageFile()
aImage.fileName = "image1.png"
aImage.fileID = 101
images.append(aImage)

aImage = imageFile()
aImage.fileName = "image1.png"
aImage.fileID = 202
images.append(aImage)

question is: how can i sort images array by 'fileID' ASC or DESC? 问题是:如何按“ fileID” ASC或DESC对图像数组进行排序?


#1楼

参考:https://stackoom.com/question/1dFJy/Swift如何按属性值对自定义对象数组进行排序


#2楼

[ Updated for Swift 3 with sort(by:) ] This, exploiting a trailing closure: [ 更新了Swift 3的sort(by :) ],利用了尾随的闭包:

images.sorted { $0.fileID < $1.fileID }

where you use < or > depending on ASC or DESC, respectively. 在何处分别使用<>取决于ASC或DESC。 If you want to modify the images array , then use the following: 如果要修改images数组 ,请使用以下命令:

images.sort { $0.fileID < $1.fileID }

If you are going to do this repeatedly and prefer to define a function, one way is: 如果要重复执行此操作,并且希望定义一个函数,则一种方法是:

func sorterForFileIDASC(this:imageFile, that:imageFile) -> Bool {
  return this.fileID > that.fileID
}

and then use as: 然后用作:

images.sort(by: sorterForFileIDASC)

#3楼

First, declare your Array as a typed array so that you can call methods when you iterate: 首先,将Array声明为类型化数组,以便在迭代时可以调用方法:

var images : [imageFile] = []

Then you can simply do: 然后,您可以简单地执行以下操作:

Swift 2 迅捷2

images.sorted({ $0.fileID > $1.fileID })

Swift 3 & Swift 4 & Swift 5 雨燕3&雨燕4&雨燕5

images.sorted(by: { $0.fileID > $1.fileID })

The example above gives desc sort order 上面的示例给出了desc排序顺序


#4楼

You can also do something like 您也可以做类似的事情

images = sorted(images) {$0.fileID > $1.fileID}

so your images array will be stored as sorted 因此您的图片数组将按排序存储


#5楼

If you are going to be sorting this array in more than one place, it may make sense to make your array type Comparable. 如果要在一个以上的地方对该数组进行排序,则使数组类型为Comparable可能有意义。

class MyImageType: Comparable, Printable {
    var fileID: Int

    // For Printable
    var description: String {
        get {
            return "ID: \(fileID)"
        }
    }

    init(fileID: Int) {
        self.fileID = fileID
    }
}

// For Comparable
func <(left: MyImageType, right: MyImageType) -> Bool {
    return left.fileID < right.fileID
}

// For Comparable
func ==(left: MyImageType, right: MyImageType) -> Bool {
    return left.fileID == right.fileID
}

let one = MyImageType(fileID: 1)
let two = MyImageType(fileID: 2)
let twoA = MyImageType(fileID: 2)
let three = MyImageType(fileID: 3)

let a1 = [one, three, two]

// return a sorted array
println(sorted(a1)) // "[ID: 1, ID: 2, ID: 3]"

var a2 = [two, one, twoA, three]

// sort the array 'in place'
sort(&a2)
println(a2) // "[ID: 1, ID: 2, ID: 2, ID: 3]"

#6楼

Nearly everyone gives how directly, let me show the evolvement: 几乎每个人都给出直接的方式,让我展示一下演变过程:

you can use the instance methods of Array: 您可以使用Array的实例方法:

// general form of closure
images.sortInPlace({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })

// types of closure's parameters and return value can be inferred by Swift, so they are omitted along with the return arrow (->)
images.sortInPlace({ image1, image2 in return image1.fileID > image2.fileID })

// Single-expression closures can implicitly return the result of their single expression by omitting the "return" keyword
images.sortInPlace({ image1, image2 in image1.fileID > image2.fileID })

// closure's argument list along with "in" keyword can be omitted, $0, $1, $2, and so on are used to refer the closure's first, second, third arguments and so on
images.sortInPlace({ $0.fileID > $1.fileID })

// the simplification of the closure is the same
images = images.sort({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })
images = images.sort({ image1, image2 in return image1.fileID > image2.fileID })
images = images.sort({ image1, image2 in image1.fileID > image2.fileID })
images = images.sort({ $0.fileID > $1.fileID })

For elaborate explanation about the working principle of sort, see The Sorted Function . 有关sort的工作原理的详细说明,请参见Sorted Function

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值