       return a.original.length - b.original.length
     return compare if compare
     a.index - b.index
-  
+
   # No matches? Sort the original array using Damerau-Levenshtein.
   if highlighted.length < 1
     highlighted = arr.reduce(

      return compare if compare
      b.index - a.index
  highlighted
  
DamerauLevenshtein = (down, across, prices={}, damerau=true) ->
  # https://github.com/cbaatz/damerau-levenshtein
  switch typeof prices.insert
    when "function"
      insert = prices.insert
    when "number"
      insert = (c) ->
        prices.insert
    else
      insert = (c) ->
        1
  switch typeof prices.remove
    when "function"
      remove = prices.remove
    when "number"
      remove = (c) ->
        prices.remove
    else
      remove = (c) ->
        1
  switch typeof prices.substitute
    when "function"
      substitute = prices.substitute
    when "number"
      substitute = (from, to) ->
        prices.substitute
    else
      substitute = (from, to) ->
        1
  switch typeof prices.transpose
    when "function"
      transpose = prices.transpose
    when "number"
      transpose = (backward, forward) ->
        prices.transpose
    else
      transpose = (backward, forward) ->
        1
# ---------------------------------------------------------------------------- #
  ds = []
  if down is across
    return 0
  else
    down = down.split("")
    down.unshift null
    across = across.split("")
    across.unshift null
    down.forEach (d, i) ->
      ds[i] = []  unless ds[i]
      across.forEach (a, j) ->
        if i is 0 and j is 0
          ds[i][j] = 0
        else if i is 0
          ds[i][j] = ds[i][j - 1] + insert(a)
        else if j is 0
          ds[i][j] = ds[i - 1][j] + remove(d)
        else
          ds[i][j] = Math.min(ds[i - 1][j] + remove(d), ds[i][j - 1] + insert(a), ds[i - 1][j - 1] + ((if d is a then 0 else substitute(d, a))))
          ds[i][j] = Math.min(ds[i][j], ds[i - 2][j - 2] + ((if d is a then 0 else transpose(d, down[i - 1]))))  if damerau and i > 1 and j > 1 and down[i - 1] is a and d is across[j - 1]
    ds[down.length - 1][across.length - 1]

        str = opts.extract(element) if opts.extract
        prev[prev.length] =
          string: str
          score: levenshtein(pattern, str)
          index: idx
          original: element
        prev
