# Chapter 8: I don't understand custom sorting with closures (pg 158 in PDF)

**URL:** <https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152>\
**Category:** Swift Apprentice\
**Created:** [March 9, 2018, 7:08am UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152 "2018-03-09T07:08:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkayuk](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkayuk/32/94323_2.png) [@dkayuk](https://forums.kodeco.com/u/dkayuk)\
**Post date:** [March 9, 2018, 7:08am UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152/1 "2018-03-09T07:08:17Z")

</div>

Hi

could someone please explain this? how does the closure work to change the sorting.

how are the two parameters $0 and $1 supplied to the closure?

many thanks  
DK

---

<div class="post-metadata">

**Author:** ![7stud](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/7stud/32/81494_2.png) [@7stud](https://forums.kodeco.com/u/7stud)\
**Post date:** [March 10, 2018, 3:49am UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152/2 "2018-03-10T03:49:30Z")

</div>

> how does the closure work to change the sorting

To sort al list, you have to compare this element to that element, then move this element over here, then start over and compare that element to this element, then switch them if necessary, then compare the second element to the fourth element, and reorder them, etc. etc. until you’ve made thousands of comparisons. The closure tells swift which of two elements in a single comparison is bigger. Here are some examples:

```
let result = ["bxa", "ab", "acd", "x"].sorted() {
    $0.count > $1.count 
}

--output:--
["bxa", "acd", "ab", "x"]

```

If `$0.count > $1.count` is true, then $0 is considered to be bigger than $1, else $1 is considered to be bigger than $0. As you can see, bigger comes first in the sorted array. To sort things, you have to know which elements are bigger and which elements are smaller.

```
let result = [[6], [1,1,1], [2,3]].sorted() {
    $0.reduce(0, +) > $1.reduce(0, +)
}

--output:--
[[6], [2, 3], [1, 1, 1]]

```

* * *

```
let result = [[6, 1], [1,3,1], [2,0,1]].sorted() {
    $0[1] > $1[1]
}

--output:--
[1, 3, 1], [6, 1], [2, 0, 1]]

```

–

```
class Money {
    let val: Int
    init(val: Int) {
        self.val = val
    }
}

let monies = [Money(val: 5), Money(val: 2), Money(val: 4)]
let results = monies.sorted() {
    $0.val > $1.val
}

--output:--
[{val 5}, {val 4}, {val 2}]

```

> How are the arguments supplied to the closure?

```
func bubbleSort(arr: [Int], sortFunc: (Int, Int) -> Bool) -> [Int] {
    var data = arr

    for i in 0..<(data.count-1) { 
        for j in 0..<(data.count-i-1) where sortFunc(data[j+1], data[j]) {
            data.swapAt(j, j + 1)
        }
    }

    return data
}

let results = bubbleSort(arr: [3, 1, 2, 4]) {
    $0 < $1
}

print(results)

```

---

<div class="post-metadata">

**Author:** ![dkayuk](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkayuk/32/94323_2.png) [@dkayuk](https://forums.kodeco.com/u/dkayuk)\
**Post date:** [March 10, 2018, 7:02am UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152/3 "2018-03-10T07:02:53Z")

</div>

Hi,

many thanks for the explanation, it was bugging me all day yesterday!

The article below also explains it quite nicely.

> **[Higher Order Functions in Swift (Sorted, Map, Filter, Reduce)](https://medium.com/@Dougly/higher-order-functions-in-swift-sorted-map-filter-reduce-dff60b5b6adf)**
>
> Higher order functions are simply functions that operate on other functions by either taking a function as an argument, or returning a…

DK

---

<div class="post-metadata">

**Author:** ![7stud](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/7stud/32/81494_2.png) [@7stud](https://forums.kodeco.com/u/7stud)\
**Post date:** [March 10, 2018, 10:23am UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152/4 "2018-03-10T10:23:50Z")

</div>

Sorry, I had to stop in the middle of my post. I wanted to post an example of a sort function, but I see you already found a good resource for “higher order” functions. I added a sort function to my previous post anyway.

---

<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:** [August 22, 2018, 11:13pm UTC](https://forums.kodeco.com/t/chapter-8-i-dont-understand-custom-sorting-with-closures-pg-158-in-pdf/40152/5 "2018-08-22T23:13:12Z")

</div>

Closed on August 22, 2018, 11:13pm UTC

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