How to Make ASObjC Sort Handle missing value?

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?

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
1 Like

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 ~~~~~~~~~~~~~~~~~~~~~~~~~

@ShaneStanley,

Thanks again for a great script. It has been running great for over two years, including my upgrade to Mojave!

But now I need for it to sort independent of character case. Can that be done?

Sure β€” just change the sorting selector:

  return (anArray's sortedArrayUsingSelector:"caseInsensitiveCompare:") as list
end sortListMakeUnique
1 Like

Perfect! Thanks for the quick reply. :+1: