• 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: Steve Thompson <email@hidden>
  • Date: Wed, 21 Jul 2010 12:35:04 +0100

Input file, testfile.csv: 

:~ steve$ cat testfile.csv 
item 1,55,blah
item 2,33,bleh
item 3,8,blurgh
item 4,494,something
item 5,29,garbage
item 6,49494,must get a proper CSV file for testing

Script:

set column_id to 2
set posix_path_to_csv to "/Users/steve/testfile.csv"
set sorted_file to do shell script ("sort -n -t , -k " & column_id & " " & posix_path_to_csv)

Result:
tell current application
do shell script "sort -n -t , -k 2 /Users/steve/testfile.csv"
--> "item 3,8,blurgh
item 5,29,garbage
item 2,33,bleh
item 1,55,blah
item 4,494,something
item 6,49494,must get a proper CSV file for testing"
end tell

Result:

"item 3,8,blurgh
item 5,29,garbage
item 2,33,bleh
item 1,55,blah
item 4,494,something
item 6,49494,must get a proper CSV file for testing"

On 21 Jul 2010, at 11:42, Richard Lake wrote:

Having trouble with this.. got a working example with "," as the separator?

On 21 Jul 2010, at 11:17, Steve Thompson wrote:


On 21 Jul 2010, at 10:26, Richard Lake wrote:

I have a CSV like the extract below that I have read into Applescript in as an array:

This is how I did something similar - I pre-process the CSV instead of sorting the array purely because it's faster. However, it's not much use if you need to sort the data by a different column at a later stage of your script

set sorted_file to do shell script ("sort -n -t , -k " & column_id & " " & posix_path_to_csv)

Just so you know, that's sort --numeric --use the next character as a separator , -sort this key"


 _______________________________________________
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>)
 >Re: Sort Array by Column X (From: Steve Thompson <email@hidden>)
 >Re: Sort Array by Column X (From: Richard Lake <email@hidden>)

  • Prev by Date: Re: Sort Array by Column X
  • Next by Date: Re: Sort Array by Column X
  • Previous by thread: Re: Sort Array by Column X
  • Next by thread: Re: Sort Array by Column X
  • Index(es):
    • Date
    • Thread