Struct not keeping tuple in order (swift3)

My Code produces a a tuples that is displayed on a label on view controller 1. I tried struct the label from vc1 to vc2 but the order is not being kept. All I want to do is replicate the exact order and the way the tuple is displayed on vc 1, on VC 2.


import UIKit

var number = [Int]()
var yourArray = [String]()

class ViewController: UIViewController {

    @IBOutlet var labez: UILabel!
    @IBOutlet var textA: UITextField!
    @IBOutlet var textB: UITextField!

    @IBAction func move(_ sender: Any) { labez.text))

    @IBAction func store(_ sender: Any) {

        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

        labez.text = { " \($0)" }.joined(separator:"\n") { " \($0)" }.joined(separator:"\n")))
    struct bad {

        static var mm = [String]()



import UIKit

class ViewController2: UIViewController {

    @IBOutlet var benCarson: UILabel!
    override func viewDidLoad() {

        benCarson.text = (String(describing:

Hi @timswift, sorry but I don’t really understand what you are trying to achieve. Could you please try to explain what the end result that you want is?

Is it to show the following in ViewController2:

(“a”, 1)
(“a”, 2)

I’m trying to follow your code but can’t make sense of why you are serialising it all to a string like you are in ViewController1. Maybe this would be a better way to write the code:

class ViewController1 {
    /// Where we store the data that the user enters
    var pairs = [(String, Int)]()
    @IBAction func store(_ sender: Any) {
        let text = textA.text ?? ""
        let num = Int(textB.text ?? "") ?? 0

        var newPairs = pairs
        newPairs.append((text, num))
        newPairs.sort { ... }
        pairs = newPairs
        labez.text = { String(describing: $0) }.joined(separator: "\n")

You shouldn’t really use the bad struct to be sharing data across the two view controllers, instead you could just pass the pairs array into ViewController2 either via prepareForSegue(...) or when you init ViewController2 depending on your implementation.

Hope that helps!

HI lianmnichols, Thanks for your response. I am trying to essentially copy and paste whatever is on vc1 to v2. I sorted all of the entries on vc1 but on v2 the way that the entries are sorted are not be kept. You code suggestion has no sort element. So I do think I can implement it.

@timswift: I left the sort element out (.sort { ... }) for you to complete as I didn’t want to type it out again :slightly_smiling_face:

Good luck!

@liamnichols could you show me how to sort it? With what I currently have I can’t figure out how to do it because I just can’t plug in let sorted = tuples.sorted(by: { this, next in.

@timswift: I can’t really understand what you are trying to achieve here and the approach in general so I think that it would be much better in the long run to take the time to understand the fundamentals of Swift. By doing this you’ll have the knowledge and understanding to take the right approach to achieve what you actually want :slight_smile:

I recommend checking out the Swift Apprentice book as a good place to start but there are also many other alternatives out there if not.

Please let me know if you have any other questions :+1:

This topic was automatically closed after 166 days. New replies are no longer allowed.