如何通过与特定键的相似性对字符串数组进行排序 [英] How to sort an array of string by similarity to specific key

查看:71
本文介绍了如何通过与特定键的相似性对字符串数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下内容:

var theArray: [String] = ["uncool", "chill", "nifty", "precooled", "dandy", "cool"]

我想按单词与关键字的相似程度对数组进行排序.

I want to sort the array by how similar the words are to the key word.

var keyWord: String = "cool"

想要的结果将是:

print// ["cool", "uncool", "precooled", ...],然后不再重要.但是,are键或contain it这两个词应该是第一个对象.

print// ["cool", "uncool", "precooled", ...] and then it does not matter anymore. But the words that are the key or contain it should be the very first objects.

到目前为止,我最接近的试用是:

My closest tryout so far has been:

let _theArray = entries.sorted { element1, element2 in

     return element1.contains(keyWord) && !element2.contains(keyWord)
}

但是这导致uncool成为第一项,然后precooled和最相关的项cool甚至出现在nifty之后.

But that results in uncool being the first item, then precooled and the most related item cool even comes after nifty.

我想念什么?非常感谢您的帮助.

What am I missing? Help is very appreciated.

推荐答案

您可以定义自己的相似性排序方法.请注意,我还添加了一个hasPrefix优先级,该优先级仅包含一个关键字,如果不想使用,可以将其删除:

You can define your own similarity sorting method. Note that I have also added a hasPrefix priority over the ones which only contains the keyword which you can just remove if you don't want it:

let theArray = ["uncool", "chill", "nifty", "precooled", "cooldaddy", "cool", "coolguy", "dandy"]
let key = "cool"

let sorted = theArray.sorted {
    if $0 == key && $1 != key {
        return true
    } 
    else if $0.hasPrefix(key) && !$1.hasPrefix(key)  {
        return true
    } 
    else if $0.hasPrefix(key) && $1.hasPrefix(key)
        && $0.count < $1.count  {
        return true
    } 
    else if $0.contains(key) && !$1.contains(key) {
        return true
    } 
    else if $0.contains(key) && $1.contains(key) 
        && $0.count < $1.count {
        return true
    }
    return false
}

print(sorted)   // ["cool", "coolguy", "cooldaddy", "uncool", "precooled", "chill", "nifty", "dandy"]

您还可以扩展Sequence并创建按键相似性排序的方法:

You can also extend Sequence and create a sorted by key similarity method:

extension Sequence where Element: StringProtocol {
    mutating func sorted<S>(by key: S) -> [Element] where S: StringProtocol {
        return sorted {
            if $0 == key && $1 != key {
                return true
            }
            else if $0.hasPrefix(key) && !$1.hasPrefix(key)  {
                return true
            }
            else if $0.hasPrefix(key) && $1.hasPrefix(key)
                && $0.count < $1.count  {
                return true
            }
            else if $0.contains(key) && !$1.contains(key) {
                return true
            }
            else if $0.contains(key) && $1.contains(key)
                && $0.count < $1.count {
                return true
            }
            return false
        }
    }
}


let sorted = theArray.sorted(by: key)  // "cool", "coolguy", "cooldaddy", "uncool", "precooled", "chill", "nifty", "dandy"]


还有变异版本:


And the mutating version as well:

extension MutableCollection where Element: StringProtocol, Self: RandomAccessCollection {
    mutating func sort<S>(by key: S) where S: StringProtocol {
        sort {
            if $0 == key && $1 != key {
                return true
            }
            else if $0.hasPrefix(key) && !$1.hasPrefix(key)  {
                return true
            }
            else if $0.hasPrefix(key) && $1.hasPrefix(key)
                    && $0.count < $1.count  {
                return true
            }
            else if $0.contains(key) && !$1.contains(key) {
                return true
            }
            else if $0.contains(key) && $1.contains(key)
                    && $0.count < $1.count {
                return true
            }
            return false
        }
    }
}

这篇关于如何通过与特定键的相似性对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆