• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sorting a list of records
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting a list of records


  • Subject: Re: Sorting a list of records
  • From: "Stockly, Ed" <email@hidden>
  • Date: Mon, 23 Aug 2010 19:33:12 -0500
  • Acceptlanguage: en-US
  • Thread-topic: Sorting a list of records

Title: Re: Sorting a list of records
On 8/23/10 3:58 PM, "Gil Dawson" <email@hidden> wrote:

> I need an algorithm to sort a list of records in a "stable" way.  By "stable",
> I mean
>
> When two keys are equal, output records are in the same order as the input.

Give this one a try. I've used it for lists but with a little find and change and a couple edits it seems to work for records.

You can use the record it builds to verify the stability.

HTH,

ES
-----------------
set recordToSort to {}
repeat with
x from 1 to 100
   set the end of recordToSort to {index:x, data:random number from 1 to 10}
end repeat
copy
recordToSort to originalRecord
set sortedRecords to SortARecord(recordToSort)

on
SortARecord(recordToSort)
    
set
filteredRecord to {}
    
set
sortRecord to {}
    
set
sortedRecord to {}
    
set
calcRecord to {}
    
set
recordSize to count of recordToSort
   repeat recordSize times
       set the end of
sortRecord to 0
       set the end of sortedRecord to {}
        
set the end of
calcRecord to {1}
    
end repeat
   set
y to 2
   repeat with i from 1 to count of recordToSort
       set thisItem to data of item i of recordToSort
       repeat with x from y to count of recordToSort
           set compItem to data of item x of recordToSort
           
            
if (
thisItem) > (compItem) then
               set
item i of calcRecord to (item i of calcRecord) + 1
           else if (thisItem) < (compItem) then
               set
item x of calcRecord to (item x of calcRecord) + 1
           end if
       end repeat
       set
y to y + 1
       if y > (count of recordToSort) then exit repeat
   end repeat
   repeat with
x from 1 to count of calcRecord
       set biggerThanCount to item x of calcRecord
       set the end of item (biggerThanCount) of sortedRecord to item x of recordToSort
       
    
end repeat
   set
finalRecord to {}
    
repeat with
thisItem in sortedRecord
       repeat with thisValue in thisItem
           set the end of finalRecord to thisValue as item
       end repeat
   end repeat
   return
finalRecord
end SortARecord
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Sorting a list of records (From: Gil Dawson <email@hidden>)

  • Prev by Date: Re: Sorting a list of records
  • Next by Date: Re: Sorting a list of records
  • Previous by thread: Re: Sorting a list of records
  • Next by thread: Re: Sorting a list of records
  • Index(es):
    • Date
    • Thread