Free AI web copilot to create summaries, insights and extended knowledge, download it at here
1582
Abstract
erties.</p><p id="f030">For debugging purposes, we conform to the <code>CustomStringConvertible</code> protocol. If you are not familiar with it, I suggest taking a look at my <a href="https://readmedium.com/what-is-the-customstringconvertible-protocol-in-swift-4b7ddbc5785b">article where I explain it in two minutes</a>.</p><p id="5314">Now let’s create a sample array of <code>Wage</code>s and shuffle it, so that we have a randomized collection:</p>
<figure id="c931">
<div>
<div>
<iframe class="gist-iframe" src="/gist/zafarivaev/c9be01f68325641c54bf47a702bbc314.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="3976">Finally, let’s implement the <code>Comparable</code> protocol to provide a sorting functionality for our <code>wages</code> array:</p>
<figure id="56e6">
<div>
<div>
<iframe class="gist-iframe" src="/gist/zafarivaev/501b315a1beeb60c24b7cb611d45aa72.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="714e">We provide a <code><</code> method to sort the collection in our custom defined ascending order.</p><p id="cbf1">Note that because we have implemented the <code><</code> method, we don’t need to define the descending <code>></code> method. Great!</p><p id="b729">With s
Options
orting done, let’s write this code to debug our implementation:</p>
<figure id="7180">
<div>
<div>
<iframe class="gist-iframe" src="/gist/zafarivaev/9060549da14ff15dadf22d5541b189b9.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="72e6">Now we have the array printed in the ascending order:</p><figure id="5384"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jJ5My4QE8O6KWlMhL5BCeg.png"><figcaption></figcaption></figure><p id="d9e5">And in the descending order:</p><figure id="3b23"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1yAhS0l3gBV_kOKTMTIiyQ.png"><figcaption></figcaption></figure><h1 id="4893">Wrapping Up</h1><p id="cb95">For more, visit the official Apple documentation:</p><div id="a2c0" class="link-block">
<a href="https://developer.apple.com/documentation/swift/comparable">
<div>
<div>
<h2>Comparable</h2>
<div><h3>The Comparable protocol is used for types that have an inherent order, such as numbers and strings. Many types in the…</h3></div>
<div><p>developer.apple.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div>
</div>
</div>
</a>
</div><p id="3d3b">Thanks for reading!</p></article></body>