title: Closest-match string-array sorting in Swift
body: <p>Using Swift4, I would like to sort a string-array according to the closest match to a given searchTerm. Important is to me that if the searchTerm can be found as an exact-match, then the returnArray should show this searchTerm upfront !</p>
  
  <p>Example: Given the <code>Array = ["Hello world", "Hello Jamaica", "Hello", "Family", "Hel"]</code></p>
  
  <p>And the <code>searchTerm = "Hello"</code>, the algorithm should return:</p>
  
  <p><code>["Hello", "Hello world", "Hello Jamaica", "Hel", "Family"]</code>.</p>
  
  <p>Approach 1: 
  I tried to use <a href="https://github.com/seanoshea/FuzzyMatchingSwift" rel="noreferrer">FuzzyMatching</a> - and it somehow worked (i.e. it did sort the inputArray according to a given searchTerm, however it did not put the exact-matches upfront ! i.e. With FuzzyMatching I achieved a good sorting according to substring-matches and syntactic sorting. But it did not bring me the exact-matches upfront in the returnArray).</p>
  
  <p>Approach 2:
  Then I tried my own algorithm - (see code below). But if there are several strings in the array that all start with my searchTerm (i.e. have searchTerm as a prefix), then somehow my algo does not a good job.</p>
  
  <pre><code>static func bestMatchFilterdStringArray(inputArray: [String], searchTerm: String) -&gt; [String] {
  
      let matchingTerms = inputArray
          .filter { $0.range(of: searchTerm, options: .caseInsensitive) != nil }
          .sorted { ($0.hasPrefix(searchTerm) ? 0 : 1) &lt; ($1.hasPrefix(searchTerm) ? 0 : 1) }
      return matchingTerms
  }
  </code></pre>
  
  <p>How is a "Closest-match string-array sorting" done in Swift4? Especially bringing me exact-matches upfront in the returnArray? Any help appreciated! </p>