• 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
Sorting an array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sorting an array


  • Subject: Sorting an array
  • From: "Robert R. Horning" <email@hidden>
  • Date: Wed, 6 Aug 2008 15:51:22 -0600

Hi scripters,

I've written a Applescript application that updates the Address Book using data extracted, using VBA, from an MS Access database running under Windows XP (under Parallels or Virtual PC) into Excel worksheets that are accessible from OS-X (Tiger) applications. The Applescript application works correctly, but requires about 10 minutes to update an Address Book containing 400-odd entries.

The first step in the application is to create an array of persons' names and id's. The persons' names in the array are then compared with persons' names in the worksheets. In order to speed up the application's execution, I'd like to sort the records in the array by persons' names before making the comparisons. The sort routine I'm trying to use was taken from a recent Script Users Digest.

Here's the routine from the Script Users Digest (with the example data slightly edited):
----
set rec to {{owner:"Dom", cats:{"Sane", "Crazy"}}, {owner:"Andy", cats:{"Hip", "Dip"}}, {owner:"Bob", cats:{"Trixie"}}}
sort(rec)
on sort(l)
if (count of l) < 2 then return l
set m to item 1 of l
set rl to rest of l
set l2 to {}
set l1 to {}
repeat with j in rl
set j to contents of j
if owner of j > owner of m then
set end of l2 to j -- this is different
else
set end of l1 to j -- so is this
end if
end repeat
return sort(l1) & {m} & sort(l2)
end sort
----
This sort routine works fine with the example data.




Below is an extract from the beginning of my application script with the sort routine added. The extract runs under the Script Editor without crashing, but the routine doesn't sort the array. Apparently the array is not of the type expected by the sort routine. What do I need to do to sort the array, hopefully without changing its internal structure?
----
set ABFullNames to {} --Full names manufactured from Address Book; includes id of each person


global ABFullNames
tell application "Address Book" to launch

(*Build a list of records containing full names and corresponding id's from the Address Book . Each full name consists of a concatenation of First, Middle, and Last names, with no added spaces or punctuation.*)

tell application "Address Book" to set firstCountOfPersons to count every person
set ABFullNames to addressBookNames()


(*Sort the list of records of names and id's.*)
log (count of ABFullNames)
log ABFullNames
sort(ABFullNames)
log ABFullNames

on addressBookNames()
set ABFullNames to {} --Full names manufactured from Address Book; includes id of each person
tell application "Address Book"
repeat with this_person in every person
if last name of this_person ≠ missing value then
set ABLastName to last name of this_person as string
if first name of this_person ≠ missing value then
set ABFirstName to first name of this_person as string
else
set ABFirstName to ""
end if

if middle name of this_person ≠ missing value then
set ABMiddleName to middle name of this_person as string
else
set ABMiddleName to ""
end if

set ABFullName to ABFirstName & ABMiddleName & ABLastName
set IndividualID to this_person's id
set ABPerson to {ABPersonName:ABFullName, Ident:IndividualID}
set end of ABFullNames to ABPerson --List of records
end if --last name of this_person ≠ missing value
end repeat --with this_person in every person
end tell --application "Address Book"
return ABFullNames
end addressBookNames


on sort(l)
	if (count of l) < 2 then return l
	set m to item 1 of l
	set rl to rest of l
	set l2 to {}
	set l1 to {}
	repeat with j in rl
		set j to contents of j
		if ABPersonName of j > ABPersonName of m then
			set end of l2 to j -- this is different
		else
			set end of l1 to j -- so is this
		end if
	end repeat
	return sort(l1) & {m} & sort(l2)
end sort
----
_______________________________________________
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
  • Follow-Ups:
    • Re: Sorting an array
      • From: Deivy Marck Petrescu <email@hidden>
  • Prev by Date: Re: Unicode Bad Characters
  • Next by Date: Re: Sorting an array
  • Previous by thread: UI Scripting a Save As Dialog
  • Next by thread: Re: Sorting an array
  • Index(es):
    • Date
    • Thread