How to Make ASObjC Sort Handle missing value?

asobjc
how-to

(Jim Underwood) #1

I’ve got a great sort handler that works fine, except if the list contains missing value. How can I make the sort handler handle/exclude/remove missing value?

Thanks.

It currently fails with this error:

-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0x7fffd48490e0

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use scripting additions


set myList to {"one", missing value, "two", "three", "four"}
set mySortedList to my sortListMakeUnique(myList)


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on sortListMakeUnique(pList) -- @List @Sort @Unique @Dups @ASObjC @Shane
  (*  VER: 1.1    2017-11-22
---------------------------------------------------------------------------------
  PURPOSE:  Remove Duplicate Items, and Sort List
  PARAMETERS:
    • pList    ┃ list  ┃ List of items
  RETURNS:  list  ┃ 
  AUTHOR:  JMichaelTX, refactored script by ShaneStanley & Chris Stone
  REF:  
    1. 2015-11-01, @ShaneStanley, Everyday ASObjC 3rd ed, p77
        See p55 for more complex sorts
—————————————————————————————————————————————————————————————————————————————————
*)
  set theSet to current application's NSSet's setWithArray:pList
  set anArray to theSet's allObjects()
  return (anArray's sortedArrayUsingSelector:"compare:") as list
end sortListMakeUnique
--~~~~~~~~~~~~~~~ END OF handler sortListMakeUnique ~~~~~~~~~~~~~~~~~~~~~~~~~


I did search Everyday AppleScriptObjC, Third Edition, by Shane Stanley, but could not find the answer. Did I miss it?


(Shane Stanley) #2

You need to filter out the offending value before you sort. Try inserting these two lines:

set thePred to current application's NSPredicate's predicateWithFormat:"SELF != nil"
set anArray to anArray's filteredArrayUsingPredicate:thePred

(Jim Underwood) #3

Thanks Shane.

Here’s the

Revised Script

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on sortListMakeUnique(pList) -- @List @Sort @Unique @Dups @ASObjC @Shane
  (*  VER: 1.2    2018-07-12
  ---------------------------------------------------------------------------------
  PURPOSE:  Remove Duplicate Items, and Sort List
  PARAMETERS:
    • pList    ┃ list  ┃ List of items
  RETURNS:  list  ┃ 
  AUTHOR:  JMichaelTX, refactored script by ShaneStanley & Chris Stone
  REF:  
    1. 2015-11-01, @ShaneStanley, Everyday ASObjC 3rd ed, p77
        See p55 for more complex sorts
    2.  2018-07-12, ShaneStanley, Late Night Software Ltd.
        How to Make ASObjC Sort Handle missing value?
        http://forum.latenightsw.com/t/how-to-make-asobjc-sort-handle-missing-value/1412/2
—————————————————————————————————————————————————————————————————————————————————
*)
  set theSet to current application's NSSet's setWithArray:pList
  set anArray to theSet's allObjects()
  
  --- Remove Array Elements with missing value ---
  set thePred to current application's NSPredicate's predicateWithFormat:"SELF != nil"
  set anArray to anArray's filteredArrayUsingPredicate:thePred
  
  return (anArray's sortedArrayUsingSelector:"compare:") as list
end sortListMakeUnique
--~~~~~~~~~~~~~~~ END OF handler sortListMakeUnique ~~~~~~~~~~~~~~~~~~~~~~~~~