<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Sorted Function on Andrew Bancroft</title>
    <link>https://www.andrewcbancroft.com/tags/sorted-function/</link>
    <description>Recent content about iOS development with Swift in Sorted Function  from Andrew Bancroft.</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Sat, 16 Aug 2014 18:06:26 +0000</lastBuildDate>
    
        <atom:link href="https://www.andrewcbancroft.com/tags/sorted-function/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Sort It Out – Sorting an Array in Swift</title>
      <link>https://www.andrewcbancroft.com/2014/08/16/sort-yourself-out-sorting-an-array-in-swift/</link>
      <pubDate>Sat, 16 Aug 2014 18:06:26 +0000</pubDate>
      
      <guid>https://www.andrewcbancroft.com/2014/08/16/sort-yourself-out-sorting-an-array-in-swift/</guid>
      <description>&lt;p&gt;I had a question come to me today regarding sorting an array of integers that are actually &lt;em&gt;encoded&lt;/em&gt; as strings in the array.  Data comes to is in a variety of encodings/types, so it&amp;rsquo;s quite common to need to adjust things to the right state for working with.  Let&amp;rsquo;s take a look at how to solve this one.&lt;/p&gt;
&lt;h2 id=&#34;the-dilemma&#34;&gt;The Dilemma&lt;/h2&gt;
&lt;p&gt;Given an array like this…&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;arrayOfIntsAsStrings&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;103&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;2&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;50&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;55&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34;98&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;… the question arises:  &amp;ldquo;How do I sort this in numerical order so that my output array is still an array of strings, but sorted like this &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;]&lt;/code&gt; (integer comparison), not this &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;]&lt;/code&gt;  (string comparison)?” Enter Swift&amp;rsquo;s &lt;code&gt;sorted&lt;/code&gt; function:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;“Swift’s standard library provides a function called sorted, which sorts an array of values of a known type, based on the output of a sorting closure that you provide. Once it completes the sorting process, the sorted function returns a new array of the same type and size as the old one, with its elements in the correct sorted order. The original array is not modified by the sorted function.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. &lt;a title=&#34;Swift iBook&#34; href=&#34;https://itun.es/us/jEUH0.l&#34; target=&#34;_blank&#34;&gt;&lt;a href=&#34;https://itun.es/us/jEUH0.l&#34;&gt;https://itun.es/us/jEUH0.l&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is exactly what we need to do the job here.&lt;/p&gt;
&lt;h2 id=&#34;the-gist&#34;&gt;The Gist&lt;/h2&gt;
&lt;p&gt;Given the original array outlined above, we can create a new_ sorted_ array like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nv&#34;&gt;sortedArray&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;sorted&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;arrayOfIntsAsStrings&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This code produces our desired output:  &lt;code&gt;[&amp;quot;1&amp;amp;#8221;, &amp;quot;2&amp;amp;#8221;, &amp;quot;50&amp;amp;#8221;, &amp;quot;55&amp;amp;#8221;, &amp;quot;98&amp;amp;#8221;, &amp;quot;103&amp;amp;#8221;, &amp;quot;1000&amp;amp;#8221;]&lt;/code&gt;&lt;/p&gt;
&lt;h2 id=&#34;thenbspdetails&#34;&gt;The Details&lt;/h2&gt;
&lt;p&gt;Swift&amp;rsquo;s sorted function takes two arguments:  an Array, and a Closure.  The part that may be confusing is the closure argument.  Isolated it looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-swift&#34; data-lang=&#34;swift&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Bool&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;in&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;str2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;toInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;ln&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Swift provides several &lt;a title=&#34;Apple Developer Documentation - Swift Closures&#34; href=&#34;https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html&#34; target=&#34;_blank&#34;&gt;shorthand forms of the closure syntax&lt;/a&gt;, but I&amp;rsquo;ve chosen the longest-form here just for full exposure and clarity.  The closure I&amp;rsquo;ve written simply needs to compute a Bool value representing the result of a comparison between two values – in this case, I&amp;rsquo;m wanting to do &lt;em&gt;integer&lt;/em&gt; comparison, so I write this:  &lt;code&gt;str1.toInt()&lt;/code&gt; and &lt;code&gt;str2.toInt()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can choose whatever parameter names you&amp;rsquo;d like, and you can actually rely on Type Inference in the parameter section — the compiler can work out what Types your parameters are.  But if it helps clue you in to what the code is doing, you can &lt;a title=&#34;Clean Coding in Swift – Type Inference&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/12/clean-coding-in-swift-type-inference/&#34; target=&#34;_blank&#34;&gt;&lt;em&gt;specify&lt;/em&gt; the Types in the closure&amp;rsquo;s parameter section, as I wrote about recently&lt;/a&gt;, and as I did in my example when I wrote &lt;code&gt;str1: String, str2: String&lt;/code&gt; (I could have left off the &lt;code&gt;: String&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Within the closure&amp;rsquo;s body, you can put in as many lines of logic as are necessary in order to produce an appropriate comparison result to get your Array in the right order.  If it gets &lt;em&gt;too&lt;/em&gt; complicated to do it in-line, think about encapsulating that logic inside one or more functions in the spirit of writing &lt;a title=&#34;Clean Coding in Swift – Functions&#34; href=&#34;http://www.andrewcbancroft.com/2014/08/07/clean-coding-in-swift-functions/&#34; target=&#34;_blank&#34;&gt;clean code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To produce an appropriate comparison result, you&amp;rsquo;ll have to consider the Types of the data you&amp;rsquo;re working with within the closure and consider any casting or manipulation you&amp;rsquo;ll need to do to produce the correct result like we did in our example. Other than that, it&amp;rsquo;s pretty straightforward to sort it out!&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>