Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!apple!dan From: dan@Apple.COM (Dan Allen) Newsgroups: comp.sys.mac.hypercard Subject: Sorting Containers (HyperTalk Script) Message-ID: <32656@apple.Apple.COM> Date: 24 Jun 89 17:15:22 GMT Distribution: comp.sys.mac.hypercard Organization: Apple Computer Inc, Cupertino, CA Lines: 38 Several people have wanted a routine to sort lines of a field or variable (containers). Here is a HyperTalk script that uses the selection sort method of sorting. Selection sorting is an algorithm of order n squared, however, for the average random cases where n < 1000, selection sorting often performs closer to n log n, so it is not bad for small sort jobs. Quicksort or Heapsort would be better for large n, but this is so simple... This script is a function. For example, if you wanted to sort field 1 of the current card, you could say: put sortContainer(field 1) into field 1 Here is the actual script: function sortContainer anyContainer -- selection sort get anyContainer put the number of lines of it into numLines repeat with i = 1 to numLines - 1 put i into k put line i of it into x repeat with j = i+1 to numLines if line j of it < x then put j into k put line j of it into x end if end repeat put line i of it into line k of it put x into line i of it end repeat return it end sortContainer Enjoy! Dan Allen HyperCard Team Apple Computer