Swift3中的排序数组 [英] Sorting Array in Swift3

查看:147
本文介绍了Swift3中的排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我具有如下结构:

In my code, I have a struct like the following:

struct Object {
    var name: String
    var count: Int

我现在正在创建一个由10个对象组成的数组,这些对象具有随机名称和随机计数.

I am now creating an array of 10 Objects with random names and random counts.

有没有一种简便的方法
a)按字母顺序对它们进行排序
b)按升序对它们进行数字排序

Is there an easy way to
a) sort them alphabetically
b) sort them numerically in ascending order

基本上,将有一个像这样的数组: [Object1, Object2, Object3]. 每个对象都有一个namecount属性,我希望该列表中的对象通过这两个属性进行排序.

Basically, there will be an array like so: [Object1, Object2, Object3]. Every Object has a name and count attribute, and I want the objects in that list be sorted via these two attributes.

Swift2中的解决方案(使用此解决方案: StackOverflow):

Solution in Swift2 (using this solution: StackOverflow):

Object.sort{
        if $0.name != $1.name {
            return $0.name < $1.name
        }
        else {
            //suits are the same
            return $0.count < $1.count
        }
    }

但是,在Swift3中,它已被重命名为sorted(by: ),我不会退出该怎么做.

However, this has been renamed to sorted(by: ) in Swift3, and I don't quit get how to do that.

推荐答案

如果要按字母顺序然后按数字顺序排序,则可以:

If you want to sort alphabetically and then numerically, you can:

var array = ["A2", "B7", "A4", "C3", "A1", "A10"]
array.sort { $0.compare($1, options: .numeric) == .orderedAscending }

会产生:

["A1","A2","A4","A10","B7","C3"]

["A1", "A2", "A4", "A10", "B7", "C3"]

我在您的数组中添加了A10,因为没有它,简单的字母排序就足够了.但是我假设您想在A4之后使用A10,在这种情况下,数字比较将为您完成工作.

I added A10 to your array, because without it, a simple alphabetic sort would have been sufficient. But I'm assuming you wanted A10 after A4, in which case the numeric comparison will do the job for you.

您已将示例更改为具有两个属性的结构.在这种情况下,您可以执行以下操作:

You changed the example to be a struct with two properties. In that case, you can do something like:

struct Foo {
    var name: String
    var count: Int
}

var array = [
    Foo(name:"A", count: 2),
    Foo(name:"B", count: 7),
    Foo(name:"A", count: 7),
    Foo(name:"C", count: 3),
    Foo(name:"A", count: 1),
    Foo(name:"A", count: 10)
]

array.sort { (object1, object2) -> Bool in
    if object1.name == object2.name {
        return object1.count < object2.count
    } else {
        return object1.name < object2.name
    }
}

或更简洁地说:

array.sort { $0.name == $1.name ? $0.count < $1.count : $0.name < $1.name }

array.sort { ($0.name, $0.count) < ($1.name, $1.count) }

请注意,不是将这个逻辑放在结尾,而是使Foo符合Comparable:

Note, rather than putting this logic in the closure, I'd actually make Foo conform to Comparable:

struct Foo {
    var name: String
    var count: Int
}

extension Foo: Equatable {
    static func ==(lhs: Foo, rhs: Foo) -> Bool {
        return (lhs.name, lhs.count) == (rhs.name, rhs.count)
    }
}

extension Foo: Comparable {
    static func <(lhs: Foo, rhs: Foo) -> Bool {
        return (lhs.name, lhs.count) < (rhs.name, rhs.count)
    }
}

这使比较逻辑很好地封装在它所属的Foo类型中.

This keeps the comparison logic nicely encapsulated within the Foo type, where it belongs.

然后,您只需执行以下操作即可将其排序:

Then you can just do the following to sort in place:

var array = ...
array.sort()

或者,如果您不想对原始数组进行排序,则可以返回一个新数组:

Or, alternatively, you can return a new array if you don't want to sort the original one in place:

let array = ...
let sortedArray = array.sorted()

这篇关于Swift3中的排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆