avatarZafar Ivaev

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>&lt;</code> method to sort the collection in our custom defined ascending order.</p><p id="cbf1">Note that because we have implemented the <code>&lt;</code> method, we don’t need to define the descending <code>&gt;</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>

When to Use the Comparable Protocol in Swift

This protocol is very handy for sorting your custom arrays

Photo by Kolar.io on Unsplash

In this brief tutorial, we will quickly explore and master the Comparable protocol in Swift.

The Comparable protocol is useful because it allows us to get a free sorting functionality once we conform our custom Array to it.

Let’s Start

Say we have a wage tracking app, so we create the Wage model:

First, we define a Month enum with a rawValue of type Int, meaning that we have each consequential month corresponding to an integer (eg. January is equal to 1, February to 2, and so on).

Next, we create a Wage structure containing the year and month properties.

For debugging purposes, we conform to the CustomStringConvertible protocol. If you are not familiar with it, I suggest taking a look at my article where I explain it in two minutes.

Now let’s create a sample array of Wages and shuffle it, so that we have a randomized collection:

Finally, let’s implement the Comparable protocol to provide a sorting functionality for our wages array:

We provide a < method to sort the collection in our custom defined ascending order.

Note that because we have implemented the < method, we don’t need to define the descending > method. Great!

With sorting done, let’s write this code to debug our implementation:

Now we have the array printed in the ascending order:

And in the descending order:

Wrapping Up

For more, visit the official Apple documentation:

Thanks for reading!

Swift
Programming
iOS
Xcode
Mobile
Recommended from ReadMedium