开发者

How I can get sorted list in way of nearer matching?

Which sorting algorithm can be used to get nearer and approximate matching list with given string input:

  1. list of strings
  2. 开发者_高级运维tobesearch_str


My understanding of what it is you want to do is as follows:

  1. You have a TCL list that contains a number of strings
  2. You have a search string that does not exactly match any of the strings in your list
  3. You want to sort the strings in the list by how close they are to the search string.

One way of measuring how close two strings are is the edit or Levevshtein distance. There is a page on the TCL wiki that gives a TCL implementaion of this algorithm. What you can then do is create a list of lists where each sub-list contains you candidate string and its distance from the search string. The code below shows how sort this list using the lsort command:

set myList [list  {AADD 3} {AABC 2} {AAAB 1} {DCBA 4}]
puts $myList
set sortedList [lsort -integer -index 1 $myList]
puts $sortedList

This results in the following output:

{AADD 3} {AABC 2} {AAAB 1} {DCBA 4}
{AAAB 1} {AABC 2} {AADD 3} {DCBA 4}

Is this the kind of thig you are after or have I misinterpreted what you are trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜