tags:

views:

32

answers:

1

Hi, which sorting algorithm can be used to get nearer and aproximate matching list with given string input: 1. list of strings 2. tobesearch_str

+5  A: 

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?

Jackson
yes that is perfet answer what I am looking for...Thanks.
OliveOne
+1 for good ESP
glenn jackman