• 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: Sort Array by Column X
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sort Array by Column X


  • Subject: Re: Sort Array by Column X
  • From: "Mark J. Reed" <email@hidden>
  • Date: Thu, 22 Jul 2010 22:12:46 -0400

Rereading your message, I noticed that you have already read your data
from the CSV text file into memory. What did you use to do that?  You
only need to worry about the complexities of CSV at the point where
you're parsing it, which it sounds like you're already doing somewhere
else...

If you already have a list of lists of values, you can just use an
all-AS implementation of your favorite sort algorithm.  Here's an
implementation of QuickSort, as found at
http://macscripter.net/viewtopic.php?id=24766.   I just modified it to
take an extra parameter indicating which column to sort by.  Note that
it sorts the list in place.


set theRows to {{"1C0096C0-613C-42E4-BF8D-768ADB7DAE5C",
"E908B072-F1F2-4A08-93E8-6F9F36E74721",
"E81FC87C-C79B-4BE2-88BC-79BB9F3B6819", "SO", "2891", "R0014C",
"SENSOREDCOMPANYNAME", "SENSORED_CONTACTNAME",
"SENSORED_CONTACTADDRESS", "SENSORED_EMAILADDRESS", "No", "7", "No",
"14/06/2010 - Fully Despatched"},
{"5C06971B-90BD-47C6-94A1-B0DA8BCFAD5B",
"8DC5A202-EE84-4E78-97C2-F0D39B2FEB99",
"C7FA819A-1E09-40FB-B274-A10DBF3A6745", "SO", "2892", "B0091C",
"SENSOREDCOMPANYNAME", "SENSORED_CONTACTNAME",
"SENSORED_CONTACTADDRESS", "SENSORED_EMAILADDRESS", "No", "7", "No",
""}}

quickSortByColumn(theRows, 5)
display dialog theRows as text
quickSortByColumn(theRows, 6)
display dialog theRows as text

on quickSortByColumn(theList, theColumn)
	--public routine, called from your script
	script bs
		property alist : theList
		property keyCol : theColumn

		on Qsort(leftIndex, rightIndex)
			--private routine called by quickSort.
			--do not call from your script!
			if rightIndex > leftIndex then
				set pivot to ((rightIndex - leftIndex) div 2) + leftIndex
				set newPivot to Qpartition(leftIndex, rightIndex, pivot)
				set theList to Qsort(leftIndex, newPivot - 1)
				set theList to Qsort(newPivot + 1, rightIndex)
			end if

		end Qsort

		on Qpartition(leftIndex, rightIndex, pivot)
			--private routine called by quickSort.
			--do not call from your script!
			set pivotValue to item (bs's keyCol) of item pivot of bs's alist
			set temp to item pivot of bs's alist
			set item pivot of bs's alist to item rightIndex of bs's alist
			set item rightIndex of bs's alist to temp
			set tempIndex to leftIndex
			repeat with pointer from leftIndex to (rightIndex - 1)
				set pointerValue to item (bs's keyCol) of item pointer of bs's alist
				if pointerValue ≤ pivotValue then
					set temp to item pointer of bs's alist
					set item pointer of bs's alist to item tempIndex of bs's alist
					set item tempIndex of bs's alist to temp
					set tempIndex to tempIndex + 1
				end if
			end repeat
			set temp to item rightIndex of bs's alist
			set item rightIndex of bs's alist to item tempIndex of bs's alist
			set item tempIndex of bs's alist to temp

			return tempIndex
		end Qpartition

	end script

	if length of bs's alist > 1 then bs's Qsort(1, length of bs's alist)
	return bs's alist
end quickSortByColumn
 _______________________________________________
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: Sort Array by Column X
      • From: Richard Lake <email@hidden>
References: 
 >Sort Array by Column X (From: Richard Lake <email@hidden>)

  • Prev by Date: Re: mail.app Getting an RSS mailbox
  • Next by Date: NoteTaker script - help needed
  • Previous by thread: Re: Sort Array by Column X
  • Next by thread: Re: Sort Array by Column X
  • Index(es):
    • Date
    • Thread