元组没有按正确的顺序排序(swift3)

时间:2017-10-27 03:49:59

标签: swift sorting struct tuples

我的代码应该使用元组来对字符串和int进行排序。字符串应该是从a-z开始的顺序,而int应该是从9-1开始的顺序。现在没有保留订单而且没有订单。

import UIKit
class ViewController: UIViewController {
    var number = [Int]()
    var yourArray = [String]()
    @IBOutlet var txtb: UITextField!
    @IBOutlet var txta: UITextField!

    @IBAction func move(_ sender: Any) {
        yourArray.append((txta.text!))
           number.append(Int(txtb.text!)!)
           let tuples = zip(yourArray,number)

            let sorted = tuples.sorted(by: { this, next in
              if this.0 < next.0 {
                   return true
            } else if this.0 == next.0 {
                   return this.1 < next.1
               } else {
                    return false
                }})
            bad.mm.append(String(describing:  sorted.map { " \($0)" }.joined(separator:"\n")))

    }}
struct bad {
    static var mm = [String]()}

enter image description here

1 个答案:

答案 0 :(得分:1)

这看起来像在工作?

let tuples:[(String,Int)] = [("baa",2), ("abc",50),("a",10)]

let result = tuples.sorted(by: { this, next in
  if this.0 < next.0 {
    return true
  } else if this.0 == next.0 {
    return this.1 < next.1
  } else {
    return false
  }})

print(result)