Sorting a list of mixed data types using ASObjC

I can use this to sort lists of strings

on Array_Sort_Strings(inputList)
	try
		set the inputList to current application's NSArray's arrayWithArray:(inputList as list)
		set the sortedItems to inputList's sortedArrayUsingSelector:"localizedStandardCompare:"
		return (sortedItems as list)
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "<Array_Sort_Strings>" & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end Array_Sort_Strings

But it will error on any numeric content. [__NSCFNumber localizedStandardCompare:]: unrecognized selector sent to instance 0x8ae7c1545a4c0538

I can sort lists of numbers with this.

on Array_Sort_Numbers(inputList)
	try
		set NSArray to current application's NSArray's arrayWithArray:(inputList as list)
		set sortedItems to NSArray's sortedArrayUsingSelector:"compare:"
		return (sortedItems as list)
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "<Array_Sort_Numbers>" & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end Array_Sort_Numbers

But it errors with any text. [NSTaggedPointerString objCType]: unrecognized selector sent to instance 0x8ae7c1545a4c3426

Is it possible to sort lists of mixed data types in ASObjC?

I also tried the following after shaking my fist at google’s AI search results and telling it to get off my lawn.

use AppleScript version "2.4"
use framework "Foundation"
set inputList to {"Banana", "Apple", "Cherry", 3, 2, 1}
Array_Sort_Mixed_Data_Types(inputList)
-->error -[__NSCFNumber _fastCStringContents:]: unrecognized selector sent to instance 0x8ae7c1545a4c0438set sortedArray to theArray's sortedArrayUsingDescriptors:{sortDescriptor}

on Array_Sort_Mixed_Data_Types(inputList)
	set NSArray to current application's NSArray's arrayWithArray:inputList
	set sortDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true selector:"compare:"
	set sortedArray to NSArray's sortedArrayUsingDescriptors:{sortDescriptor}
	return sortedArray as list
end Array_Sort_Mixed_Data_Types

My suggestion:

use framework "Foundation"
use scripting additions

set inputList to {4, "Banana", "Apple", "Cherry", 3, 2, 1}

sortMixedArray(inputList) --> {"Apple", "Banana", "Cherry", 1, 2, 3, 4}

on sortMixedArray(inputList)
	set the inputList to current application's NSArray's arrayWithArray:inputList
	set descriptorOne to current application's NSSortDescriptor's sortDescriptorWithKey:"integerValue" ascending:true selector:"compare:"
	set descriptorTwo to current application's NSSortDescriptor's sortDescriptorWithKey:"stringValue" ascending:true selector:"localizedStandardCompare:"
	set sortedArray to (inputList's sortedArrayUsingDescriptors:{descriptorOne, descriptorTwo}) --string's first
	# 	set sortedArray to (inputList's sortedArrayUsingDescriptors:{descriptorTwo, descriptorOne})
	return sortedArray as list
end sortMixedArray

BTW, I assume that the goal is to use different methods to sort strings and integers. Otherwise, the following appears to work:

use framework "Foundation"
use scripting additions

set inputList to {4, "Banana", "Apple", "Cherry", 3, 2, 1, 11, 10, 20}

sortMixedArray(inputList) --> {1, 2, 3, 4, 10, 11, 20, "Apple", "Banana", "Cherry"}

on sortMixedArray(inputList)
	set the inputList to current application's NSArray's arrayWithArray:inputList
	set descriptorOne to current application's NSSortDescriptor's sortDescriptorWithKey:"stringValue" ascending:true selector:"localizedStandardCompare:"
	set sortedArray to (inputList's sortedArrayUsingDescriptors:{descriptorOne})
	return sortedArray as list
end sortMixedArray

Not currently, but I appreciate you sharing both methods! I’ll likely need both.

Thanks!