Hey there Richard,
There are a variety of ways to skin this cat. I'd do it in Python personally since I'm familiar with it and it has a csv library that is a fast and convenient way to get data. Since this is an AppleScript list though I'll give a suggestion for a recklessly inefficient means of sorting your data.
What I would do is this: 1) Store the data for each row in a record with named elements. Include an element in the record for row index. Like: {rowIdx:1, custGUID:"1234", formGUID:"5678", contactGUID:"901234", etc...} 2) Store the row records in a list. Name is unsortedList or something. 3) Initialize an empty list which will be used to hold the sorted data. Name is sortedList 4) Iterate through the unsortedList and find the record that should be first in your sorted list. Remove that record from unsortedList and insert it at the end of sortedList. 5) Repeat step 4 until unsortedList is empty.
At the end of this unsortedList will be empty and sortedList will be full and ordered by whatever point of data you want. It is, as mentioned, ridiculously inefficient. You'll touch every record in the unsortedList at least n times (where n is the length of the unsortedList). If you're talking about millions of rows and doing this over and over again in your code then you'll want to have a better solution than doing it purely in AppleScript.
If that is the case then you might want to consider storing your data in a database. Sqlite comes to mind. In SQL it's trivial to query data from a table using an ORDER BY clause.
Good luck! Zack On Oct 5, 2010, at 8:29 AM, Richard Lake wrote: Hello,
I am returning to an old issue from this summer that never got resolved, it involves reading a .csv file into an array, sorting the array by a column and then writing the array back to disc. The input and output delimited must be retained always.
I require a simple routine that can be called at will which will sort Column 5 in descending order. This column can also contain nothing but empty quotes.
Sometime ago I was a given a solution that involved the use of unix commands Head, Tail, Tr and Sort. This refused to work no matter what I tried. For starters -n argument of Head wouldn't work I had to use -c bytenumber instead, but the list never got sorted anyway!
Column Headings and example of how CSV file is stored.
"Customer GUID","Form GUID","Contact GUID","Form Type","Ref No","Account Number","Company Name","Contact Name","Contact Address","Email address","Artwork awaiting Response","Days no Response","Special Pricing","Date" "17F1098A-482E-4BCB-BC17-B56094FF7CC1","ADB3CC97-0A42-40FF-9C63-10AB043A0F7E","66864065-0256-41E3-AFCF-CC7053DAD196","PF","","S0154C","Synectic Systems Group Ltd","A Name","","email@address","Yes","2","No","" "43C79C66-2127-41B9-B444-34EAB49DEB68","7AF809BA-10F9-4997-858D-DF3ACE829BC0","ED9FF7CF-ECF0-4D2D-BAB9-687B4AEEF109","QT","2940","M0012C","Manchester Metropolitan University[] The","A Name","An Address[]2nd Line[]3rd Line[]Etc[]","email@address","No","1","No","" "41A9C623-3E6E-44E1-81D6-FC5B57FCC7D8","4625DC32-B013-42CC-BFD4-73962A3106F4","1D54FDFE-731E-4B85-BD7B-416A7B05AA90","SO","3110","C0250C","Chem-dry (uk) Ltd","A Name","An Address[]2nd Line[]3rd Line[]Etc[]","email@address","Yes","2","No",""
The array retains its data in this fashion and I have no plans to change this behaviour as its used countless times as part of a much wider application.
set formData to {{"7C925871-2545-458E-9E1A-F3362F5010BE", "CFB29657-ABBA-4AAC-83CF-F436D1AFE3BE", "2ADC20E6-6AB3-4FD9-B137-BAD34D1B580D", "QT", "2942", "C0067C", "Conocophillips", "A Name", "An Address[]2nd Line[]3rd Line[]Etc[]", "email@address", "No", "1", "No", ""}, {"7C925871-2545-458E-9E1A-F3362F5010BE", "97A6977D-B0F4-480B-ACA5-9977E7307996", "2CAC23D0-AEF7-4194-AD1C-201B053298A0", "SO", "3111", "C0067C", "Conocophillips", "A Name", "An Address[]2nd Line[]3rd Line[]Etc[]", "aemail@address", "No", "1", "No", ""}, {"059B2178-D55B-4542-A794-0B4079F6B007", "C6484FD1-989C-4DA5-8AF8-161769BB1146", "5734F334-2851-496A-82D0-B6A7658ACA53", "SO", "3112", "B0063C", "Exova", "A Name", "An Address[]2nd Line[]3rd Line[]Etc[]", "email@address", "Yes", "1", "Yes", ""}}
----
If anyone can offer a working solution I'd like to hear it.
Richard Lake Marketing Manager Tel: 0800 158 3898 | Email: email@hidden
zack is one amazingly cool frood
|