Comparing 2 numeric strings

Is the method compareObject:toObject: a good candidate for that?
I just need the comparison result. Not the value.
Can someone give an example of the syntax to use?

Thanks.

Found it!

use framework "Foundation"
use framework "AppKit"
use scripting additions

set theDescriptor to (current application's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true)
theDescriptor's compareObject:string1 toObject:string2

Probably more for my own learning here, but if the comparison being sought is one of numerical magnitude (i.e. answering the question, “Which of two strings when evaluated numerically is the greater?”), then would using AppleScript’s Greater Than/Less Than numerical operators not suffice ?

For example:

set a to "12345"
set b to "12344"

return (a > b) --> true

This yields an incorrect result for string representations of negative values, as does compareObject:toObject:, but coercions will resolve correctly, I believe:

use framework "Foundation"

return (current application's NSSortDescriptor's ¬
	sortDescriptorWithKey:"self" ascending:true)'s ¬
	compareObject:"-3.1415" toObject:"-3.1425"
        --> -1 (incorrect)
"-3.1415" > "-3.1425" --> false (incorrect)
"-3.1415" as number > "-3.1425" as number --> true (correct)

What is your OS version?
Here on Sierra, the method works well with negative numbers.

I’m using this method to compare strings that can contain numbers, not numbers as strings:
Try those examples:

use framework "Foundation"

set theDes to (current application's NSSortDescriptor's sortDescriptorWithKey:"self" ascending:true)
theDes's compareObject:"mississippi" toObject:"colorado" --> 1 (correct)
theDes's compareObject:"File 20181203.pdf" toObject:"File 20190312.txt" --> -1 (correct)
theDes's compareObject:"version 7.2b" toObject:"version 7.3" --> -1 (correct)
theDes's compareObject:"-2,01 mm" toObject:"2,01 mm" --> -1 (correct)
theDes's compareObject:(-3.445 as string) toObject:(-3.445 as string) --> 0 (correct)
theDes's compareObject:"-1 344,34" toObject:"-1 344,34" --> 0 (correct) French localization
theDes's compareObject:"-1,344.34" toObject:"-1,344.34" --> 0 (correct) English localization

MacOS 10.12.6 (16G1815)
Script Debugger 7.0.7 (7A72)
Script Editor 2.9 (191)

Hi.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set a to "2345"
set b to "12344"

(a > b) -- true ("2" > "1")
considering numeric strings
	(a > b) -- false
	(a < b) -- true
end considering

set a to current application's class "NSString"'s stringWithString:(a)
((a's compare:(b) options:(current application's NSNumericSearch) range:({0, a's |length|()})) = current application's NSOrderedDescending) -- false (a not > b)
((a's compare:(b) options:(current application's NSNumericSearch) range:({0, a's |length|()})) = current application's NSOrderedSame) -- false (a ≠ b)
((a's compare:(b) options:(current application's NSNumericSearch) range:({0, a's |length|()})) = current application's NSOrderedAscending) -- true (a < b)
1 Like

Ah, I see.

:+1:t3: Thank you.


System info: AppleScript version: 2.7 System version: 10.13.6
Application versions: Script Editor: 2.10 Script Debugger: 7.0.4