I am writing a script that exports the Address Book. Is there a simple way to read this list in a sorted order?
The names I am reading now appear to be in a random order (maybe the order they we entered).

If I am going to have to sort the list myself can someone point me to a good code snippet for this.

Thanks

This is a very fast QuickSort algorithm AppleScripted by Nigel Garvey and Arthur Knapp:

``````qsort(l, 1, -1)

on qsort(theList, l, r)
-- Nigel Garvey & Arthur Knapp. Sorts the list in place so copy before testing.
-- Save the original to a new variable if you want to keep it unaltered.
script o
property cutoff : 10
property p : theList

on qsrt(l, r)
set i to l
set j to r
set v to my p's item ((l + r) div 2)

repeat while (j > i)
set u to my p's item i
repeat while (u < v)
set i to i + 1
set u to my p's item i
end repeat

set w to my p's item j
repeat while (w > v)
set j to j - 1
set w to my p's item j
end repeat

if (i > j) then
else
set my p's item i to w
set my p's item j to u
set i to i + 1
set j to j - 1
end if
end repeat

if (j - l < cutoff) then
else
qsrt(l, j)
end if

if (r - i < cutoff) then
else
qsrt(i, r)
end if
end qsrt

on isrt(l, r)
set x to l
set z to l + cutoff - 1
if (z > r) then set z to r

set v to my p's item x
repeat with y from (x + 1) to z
if (my p's item y < v) then
set x to y
set v to my p's item y
end if
end repeat

tell my p's item l
set my p's item l to v
set my p's item x to it
end tell

set u to my p's item (l + 1)
repeat with i from (l + 2) to r
set v to my p's item i
if (v < u) then
set my p's item i to u
repeat with j from (i - 2) to l by -1
if (v < my p's item j) then
set my p's item (j + 1) to my p's item j
else
set my p's item (j + 1) to v
exit repeat
end if
end repeat
else
set u to v
end if
end repeat
end isrt
end script

set listLen to (count theList)
if (listLen > 1) then -- otherwise the handler will error
-- Translate negative indices
if (l < 0) then set l to listLen + l + 1
if (r < 0) then set r to listLen + r + 1

if (r = l) then
-- No point in sorting just one item
else
-- Transpose transposed indices
if (l > r) then
set temp to l
set l to r
set r to temp
end if

if (r - l < o's cutoff) then
-- Skip the Quicksort if cutoff or less items
else
o's qsrt(l, r)
end if
o's isrt(l, r)
end if
end if
end qsort
``````

For my curiosity, how are you exporting?
Would you mind posting that code?

Thanks,

Craig

Thanks Nigel.

Craig, this is very rough but the basics are:

``````tell application "Address Book"
set OutputList to {}
set CurPerson to ""
set peopleCount to (count every person)
repeat with i from 1 to peopleCount

set PersonsName to ""
set CompamyName to ""
set label1 to "" --
set PhoneNum1 to ""

try
set PersonsName to (name of person i)
set CompanyName to (organization of person i)
set label1 to "(" & the first character of (label of first phone of person i as string) & ") "
set PhoneNum1 to (value of first phone of person i)
end try

set CurPerson to PersonsName & "," & CompanyName & "," & label1 & PhoneNum1

set OutputList to OutputList & CurPerson

end repeat

end tell
``````