What is Best Method for Sorting List of Objects by Object Property?
For example, I’d like to sort a list of Note objects, from an Evernote search or selection, by one of the date properties of the Note.
EDIT: Mon, Feb 22, 2016 at 10:44 PM CST
See answer and final script here:
The below script does this by:
- Looping through the original Note list, putting the index and date into an AppleScript record.
- Sorting this list of records using the ASObjC Runner “rearrange records” command
- To process the Notes in sorted order, I use the record “index” property to access the actual Note object in the original Note list.
I am a little concerned about performance, since this took 17.2 seconds to sort 2,563 notes/records.
Is there a better method to do this?
@Shane Stanley:
¢ Is there a replacement for the ASObjC Runner “rearrange records” command in one of your script libraries?
¢ I looked but did not find one.
¢ You have previously stated that Runner may not work post El Capitan
I apologize for the sloppiness of this code, but it is just a quick-and-dirty script to identify the best method for sorting objects.
set cmdStr to "perl -e 'use Time::HiRes qw(time); print time'"
set timeStart to do shell script cmdStr
set noteRecList to {}
--- GET LIST OF NOTES FROM EVERNOTE ---
tell application "Evernote"
--set selNotes to selection
-- OR --
set selNotes to (find notes "tag:.NB.Personal")
set numNotes to count of selNotes
log "Num of Notes: " & numNotes
display notification "Number of Notes: " & numNotes
set noteIndex to 0
repeat with oNote in selNotes
set noteIndex to noteIndex + 1
set noteTitleStr to title of oNote
set creationDate to creation date of oNote
set modificationDate to modification date of oNote
--- ADD TO LIST OF RECORDS SO NOTES CAN BE SORTED ON DATE ---
copy {index:noteIndex, modDate:modificationDate, noteTitle:noteTitleStr} to the end of noteRecList
--noteObj:oNote, --> when I included the oNote object, the rearrange records got error.
end repeat
end tell
set noteRec to item 1 of noteRecList
log "~~~ INFO OF FIRST NOTE PRIOR TO SORT ~~~"
log modDate of noteRec
log noteTitle of noteRec
--- SORT THE NOTE RECORD LIST ---
tell application "ASObjC Runner"
set sortedList to rearrange records noteRecList by keys "modDate" with ascending orders
end tell
log "
~~~~ AFTER SORT ~~~~
"
--- GET THE 1st NOTE IN THE SORTED LIST, WHICH SHOULD BE THE OLDEST ---
set noteRec to item 1 of sortedList
log modDate of noteRec
log noteTitle of noteRec
log index of noteRec
set noteIndex to (index of noteRec)
--- NOW GET THE SAME NOTE OBJECT FROM THE EVERNOTE NOTE LIST ---
tell application "Evernote"
set oNote to (item noteIndex of selNotes)
log "~~~ CONFIRM SELECTION OF NOTE OBJECT ~~~"
log (modification date of oNote as string)
log (title of oNote as string)
end tell
set timeStop to do shell script cmdStr
set executionTime to (round ((timeStop - timeStart) * 1000)) / 1000.0
log "Execution Time: " & executionTime & " sec"
LOG PANEL
(*Num of Notes: 2563*)
(*~~~ INFO OF FIRST NOTE PRIOR TO SORT ~~~*)
(*date Sat, Feb 20, 2016 at 11:02 PM*)
(*New Books Dec 26*)
(*
~~~~ AFTER SORT ~~~~
*)
(*date Fri, Feb 10, 1950 at 11:10 PM*)
(*TEST Script Sort*)
(*2563*)
(*~~~ CONFIRM SELECTION OF NOTE OBJECT ~~~*)
(*Fri, Feb 10, 1950 at 11:10 PM*)
(*TEST Script Sort*)
(*Execution Time: 17.206 sec*)