public PartialViewResult EditDistance(string tags, string search) { //add in delim characters char[] delim = { ',', '.', ' ', ';' }; //get rid of white space if there is any tags.Replace(" ", string.Empty); search.Replace(" ", string.Empty); //split the tags with the defined delimiters string[] seperated = tags.Split(delim); tag[] terms = new tag[seperated.Length]; //loop through and compute the Levenshtein distance with each tag for (int i = 0; i < seperated.Length; i++) { terms[i] = new tag(); terms[i].Name = seperated[i]; terms[i].MinDistance = ComputeLevenshtein(search, seperated[i]); } //sort the items from least to greatest Sort(terms); //pass the terms to the view to display ViewBag.result = terms; return PartialView("SearchResults"); }
//simple bubble sort: innefficient public void Sort(tag[] terms) { tag temp = new tag(); for(int i=0; i<terms.Length; i++) for(int j=i+1; j<terms.Length; j++) { if(terms[i].MinDistance > terms[j].MinDistance) { temp = terms[i]; terms[i] = terms[j]; terms[j] = temp; } } }