Quick Text Sorting Handler

Following script is designed for the cases when the text is not saved in the text file.


set theText to "ARG_MAX indeed limits the total size of
 the command line and the environment,
 but you're facing an additional limitation:
 one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"

-- or:
-- set theText to (get the clipboard as text)

repeat 8 times -- making big text
	set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
	script o
		use AppleScript version "2.4" -- Yosemite (10.10) or later
		use framework "Foundation"
		use scripting additions
		on AsObjCsort(theList)
			-- convert list to Cocoa array
			set theArray to current application's NSArray's arrayWithArray:theList
			-- sort the array using a specific function
			set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
			-- return the sorted array as an AppleScript list
			return theArray as list
		end AsObjCsort
		on sortText(theText as text)
			set ATID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to return
			try
				set sortedTextList to paragraphs of (do shell script "echo " & quoted form of theText & " | sort")
				set AppleScript's text item delimiters to ATID
				return sortedTextList
			on error -- the text is too big for shell sorting.
				set AppleScript's text item delimiters to ATID
				return o's AsObjCsort(get paragraphs of theText)
			end try
		end sortText
	end script
	o's sortText(theText)
end sortText

So why not use th ASObjC version all the time?

use scripting additions
set theText to "ARG_MAX indeed limits the total size of
 the command line and the environment,
 but you're facing an additional limitation:
 one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"

repeat 8 times -- making big text
	set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
	script o
		use AppleScript version "2.4" -- Yosemite (10.10) or later
		use framework "Foundation"

		on AsObjCsort(theList)
			-- convert list to Cocoa array
			set theArray to current application's NSArray's arrayWithArray:theList
			-- sort the array using a specific function
			set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
			-- return the sorted array as an AppleScript list
			return theArray as list
		end AsObjCsort
	end script
	return o's AsObjCsort(paragraphs of theText)
end sortText

I’m assuming so it would work on Versions older than Yosemite?

Also

Here is a modified version of your code with the second “on sortText(theText as text)” subroutine moved out of the script object. (one less recursive call)

use scripting additions
set theText to "ARG_MAX indeed limits the total size of
 the command line and the environment,
 but you're facing an additional limitation:
 one argument must not be longer than MAX_ARG_STRLEN
(which is unfortunately hard-coded to be 131072).
See https://unix.stackexchange.com/questions/120642/what-defines
-the-maximum-size-for-a-command-single-argument"

-- or:
-- set theText to (get the clipboard as text)

repeat 8 times -- making big text
	set theText to theText & theText
end repeat
-- return length of theText --> 1388544 (about 1,4 MB) with 12 times repeat loop

my sortText(theText as text)

on sortText(theText as text)
	script o
		use AppleScript version "2.4" -- Yosemite (10.10) or later
		use framework "Foundation"
		on AsObjCsort(theList)
			-- convert list to Cocoa array
			set theArray to current application's NSArray's arrayWithArray:theList
			-- sort the array using a specific function
			set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
			-- return the sorted array as an AppleScript list
			return theArray as list
		end AsObjCsort
	end script
	set ATID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return
	try
		set sortedTextList to paragraphs of (do shell script "echo " & quoted form of theText & " | sort")
		set AppleScript's text item delimiters to ATID
		return sortedTextList
	on error -- the text is too big for shell sorting.
		set AppleScript's text item delimiters to ATID
		return o's AsObjCsort(get paragraphs of theText)
	end try
end sortText

Thank you. The change you made is helpful.
.

Not for this reason. As I checked, the shell sorting (3 ms) is little faster then AsObjC sorting (10 ms).