A bubble sort algorithm in Lingo. For use when Director's built in sort function is not sufficient.
-- bubbleSortForStrings AND bubbleSortForStringLists
-- (c) 2003 Michael Nieves
-- bubbleSortForStrings(stringList)
-- returns list sorted alpha-numerically
-- use this function for one-dimensional lists of strings
-- bubbleSortForStringLists(list, targetIndex)
-- returns list sorted alpha-numerically on the targeted index
-- use this function for two-dimensional lists
-- that need to sorted on a particular field
-- for example: [["C",3],["B",2],["A",1]]
-- could be sorted by: bubbleSortForStringLists([["C",3],["B",2],["A",1]], 1)
-- which would return: [["A", 1], ["B", 2], ["C", 3]]
on bubbleSortForStrings(strList)
stillSorting = TRUE
iterationCount = 0
repeat while(stillSorting)
stillSorting = FALSE
repeat with i = 1 to strList.count - 1
newPair = sortPair([strList[i], strList[i + 1]])
if newPair[1] <> strList[i] then
strList[i] = newPair[1]
strList[i + 1] = newPair[2]
stillSorting = TRUE
end if
end repeat
end repeat
return strList
end
on sortPair(unsortedStringList)
if unsortedStringList.count <> 2 then
alert("sortPair function requires a list of two strings.")
end if
if unsortedStringList[1].length > unsortedStringList[2].length then
longestCharLength = unsortedStringList[2].length
else
longestCharLength = unsortedStringList[1].length
end if
repeat with charIndex = 1 to longestCharLength
if unsortedStringList[1].char[charIndex] <> unsortedStringList[2].char[charIndex] then
repeat with i = 1 to sortOrder.length
if sortOrder.char[i] = unsortedStringList[1].char[charIndex] then
return [unsortedStringList[1], unsortedStringList[2]]
end if
if sortOrder.char[i] = unsortedStringList[2].char[charIndex] then
return [unsortedStringList[2], unsortedStringList[1]]
end if
end repeat
end if
end repeat
return [value(unsortedStringList[1]), value(unsortedStringList[2])]
end
on bubbleSortForStringLists(strList, targetIndex)
stillSorting = TRUE
iterationCount = 0
repeat while(stillSorting)
stillSorting = FALSE
repeat with i = 1 to strList.count - 1
newPair = sortListPair([strList[i], strList[i + 1]], targetIndex)
if newPair[1] <> strList[i] then
strList[i] = newPair[1]
strList[i + 1] = newPair[2]
stillSorting = TRUE
end if
end repeat
end repeat
return strList
end
on sortListPair(unsortedStringList, targetIndex)
if unsortedStringList.count <> 2 then
alert("sortPair function requires a list of two strings.")
end if
if unsortedStringList[1][targetIndex].length > unsortedStringList[2][targetIndex].length then
longestCharLength = unsortedStringList[2][targetIndex].length
else
longestCharLength = unsortedStringList[1][targetIndex].length
end if
repeat with charIndex = 1 to longestCharLength
if unsortedStringList[1][targetIndex].char[charIndex] <> unsortedStringList[2][targetIndex].char[charIndex] then
repeat with i = 1 to sortOrder.length
if sortOrder.char[i] = unsortedStringList[1][targetIndex].char[charIndex] then
return [unsortedStringList[1], unsortedStringList[2]]
end if
if sortOrder.char[i] = unsortedStringList[2][targetIndex].char[charIndex] then
return [unsortedStringList[2], unsortedStringList[1]]
end if
end repeat
end if
end repeat
return [value(unsortedStringList[1]), value(unsortedStringList[2])]
end
Contact
MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA