# Struct not keeping sorted order (swift4)

**URL:** <https://forums.kodeco.com/t/struct-not-keeping-sorted-order-swift4/35060>\
**Category:** Swift\
**Created:** [October 1, 2017, 7:26pm UTC](https://forums.kodeco.com/t/struct-not-keeping-sorted-order-swift4/35060 "2017-10-01T19:26:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [October 1, 2017, 7:26pm UTC](https://forums.kodeco.com/t/struct-not-keeping-sorted-order-swift4/35060/1 "2017-10-01T19:26:33Z")

</div>

My code has 2 textfields one for int and the other for string. The way the code is supposed to be sorted is by ascending alphabetical order for the string (a-z)then by descending order for the int (9-1).So i entered 2 entries in a,2 a,1 order but the way the list is being displayed is a,2 a,1 which is not the sorted order. How can I keep the sorted order?

VIEWCONTROLLER

```
   @IBAction func move(_ sender: Any) {
    yourArray.append((textA.text!))
    number.append(Int(textB.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]()}

```

VIEWCONTROLLER2

```
   override func viewDidLoad() {
super.viewDidLoad()

let defaults = UserDefaults.standard
defaults.set(benCarson.text, forKey: "SavedStringArray2")
defaults.synchronize()

benCarson.text = String(describing: bad.mm)
benCarson.numberOfLines = 5000

    }

```

Picture of result **[https://i.stack.imgur.com/7MM4l.jpg](https://i.stack.imgur.com/7MM4l.jpg)**

---

<div class="post-metadata">

**Author:** ![gargoyle](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gargoyle/32/107285_2.png) [@gargoyle](https://forums.kodeco.com/u/gargoyle)\
**Post date:** [October 25, 2017, 6:45am UTC](https://forums.kodeco.com/t/struct-not-keeping-sorted-order-swift4/35060/2 "2017-10-25T06:45:05Z")

</div>

Your question says you want to first sort by the name in ASCENDING order, and then by the number in DESCENDING order…and that’s exactly what you showed, so I’m not sure what you are saying your issue is. You should write your method like this though, since you’re comparing strings and you should handle localization instead of just assuming ascii ordering.

```
let sorted = tuples.sorted {
    let result = $0.0.localizedCaseInsensitiveCompare($1.0)
    if result == .orderedSame {
        return $0.1 > $1.1
    } else {
        return result == .orderedAscending
    }
}

```

If you really want it to be numerically ASCENDING then just change the \> to a \< in the sort.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [March 17, 2018, 11:26am UTC](https://forums.kodeco.com/t/struct-not-keeping-sorted-order-swift4/35060/3 "2018-03-17T11:26:41Z")

</div>

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