Search content:

 

Personal Menu
Username:
Password:
Save password

Become a member

Forgot Password?

 

Don't miss these
DOMAJ Site and Forum
QTMovieXtra
Sending a e-mail with attachment using CDO and VbScriptXtra
Convert between bases-Integer to String
Check QuickTime Version
Giving a shadow for a sprite
Detect MS Media Player v9
Buddy Saver
Mastering Macromedia Dreamweaver 3
Text Search Engine
MediaMacros Xtras Mall
 

 

 

Behavior Bubble Sort Algorithm (for Strings)

Added on 5/16/2003

 

Compatibilities:
D7 D8 D8_5

This item has not yet been rated

Author: Magnamerc

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
  
  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
  
  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
  
  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 [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

 


Upload Provided by ABCUpload ASP

Contact

MMI
22 West Court Sq
Suite 2C
Newnan, GA 30263
USA

Fax - (206) 339-5833

Send e-mail