Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
FileFlex
Importer
MUI Maker
Elapsed Time
Direct TTS (Text to speech)
Set the blend of Sprite
KeyboardControl Xtra Lite
Cleanup Users Components
Import image (at runtime)
WebCamXtra - Computer Vision & Camera Tracking
 

 

 

Behavior bubbleSortForStrings AND bubbleSortForStringLists

Added on 10/6/2003

 

Compatibilities:
Script

This item has not yet been rated

Author: Magnamerc

A bubblesort algorithm in Lingo. Updated 10/6/2003

-- bubbleSortForStrings AND bubbleSortForStringLists
-- (c) 2003 Michael Nieves
-- Updated 10/6/2003

-- 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] = unsortedStringList[2] then
    return(unsortedStringList)
  end if
  
  sortOrder = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  
  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
  
  if unsortedStringList[1].length > unsortedStringList[2].length then
    return [unsortedStringList[2], unsortedStringList[1]]
  else
    return [unsortedStringList[1], unsortedStringList[2]]
  end if
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].duplicate(), strList[i + 1].duplicate()], targetIndex)
      if newPair[1][targetIndex] <> strList[i][targetIndex] then
        strList[i] = newPair[1].duplicate()
        strList[i + 1] = newPair[2].duplicate()
        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] = unsortedStringList[2][targetIndex] then
    return(unsortedStringList)
  end if
  
  sortOrder = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  
  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 [value(unsortedStringList[1]), value(unsortedStringList[2])]
        end if
        
        if sortOrder.char[i] = unsortedStringList[2][targetIndex].char[charIndex] then
          return [value(unsortedStringList[2]), value(unsortedStringList[1])]
        end if
      end repeat      
    end if
  end repeat
  
  if unsortedStringList[1][targetIndex].length > unsortedStringList[2][targetIndex].length then
    return [value(unsortedStringList[2]), value(unsortedStringList[1])]
  else
    return [value(unsortedStringList[1]), value(unsortedStringList[2])]
  end if
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail