官术网_书友最值得收藏!

  • iOS Programming Cookbook
  • Hossam Ghareeb
  • 175字
  • 2021-07-09 18:29:37

How it works...

Swift provides us with a built-in system function called sort. The function can sort any collection of data. The function, by default, will sort the collection in an ascending order. The sort function gives us another flexibility by which you can provide a closure that returns the comparison result between any two items in the list to determine which should come first in the list.

As we saw, the default sort function sorts our data in an ascending order; in order to do any other logic, we can sort with closure that gives you two items as parameters to decide how to compare them. The sort function sorts the collection in place, and that's why the names variable is created as var not let. If the names collection is defined as let, you will not be able to use the sort() function. There is another function called sorted(), which returns a totally new sorted collection without changing the original one. It's available in both versions of the collection with var or let.

主站蜘蛛池模板: 云安县| 尤溪县| 高碑店市| 永城市| 绥阳县| 彰化县| 黄浦区| 夏邑县| 钟山县| 华蓥市| 白水县| 商都县| 常山县| 祁门县| 定远县| 静安区| 永清县| 环江| 祥云县| 林周县| 论坛| 清原| 开阳县| 海丰县| 肥城市| 建宁县| 盘锦市| 沈阳市| 崇左市| 金乡县| 五华县| 克东县| 石林| 常宁市| 泽普县| 竹北市| 莲花县| 长治县| 桐柏县| 泾川县| 杨浦区|