Copy link to clipboard
Copied
Hi every,
Has anyone experience with StringA.localeCompare(StringB) method?
Does it need any library or preferences to work?
Jarek
Jarek -- You're right. I thought it worked ok because:
'b'.localeCompare('b') // returns 0
'b'.localeCompare('a') // returns 1
'b'.localeCompare('c') // returns -1
Setting the locale to Polish doesn't help (as it shouldn't, but you never know), nor various variants of the locale string (pl, PL, pl_PL): "Ä…".localeCompare("z", "pl") keeps returning 1 instead of -1. The examples shown in that link don't work either. Don't know how to make it work. Sorry.
Peter
Copy link to clipboard
Copied
You don't need libraries, it's used to compare plain strings. See here: String.prototype.localeCompare() - JavaScript | MDN
Peter
Copy link to clipboard
Copied
Hi Peter,
Thanks for the link.
Does InDesign javascript support this method options?
I am testing with "locale = pl" but
"a".localeCompare("z", "pl") // returns -1
"Ä…".localeCompare("z", "pl") // returns 1
From my side ID Obj Model description: "Performs a localized comparison of two strings" is not true...
Jarek
Copy link to clipboard
Copied
Jarek -- You're right. I thought it worked ok because:
'b'.localeCompare('b') // returns 0
'b'.localeCompare('a') // returns 1
'b'.localeCompare('c') // returns -1
Setting the locale to Polish doesn't help (as it shouldn't, but you never know), nor various variants of the locale string (pl, PL, pl_PL): "Ä…".localeCompare("z", "pl") keeps returning 1 instead of -1. The examples shown in that link don't work either. Don't know how to make it work. Sorry.
Peter
Copy link to clipboard
Copied
Hi,
I found this link (Marc Autret page)
Indiscripts :: Alphabetical Sort in JavaScript (and InDesign)
as very helpful and let me share it for future searches
Jarek
Copy link to clipboard
Copied
Thanks Jarek for the mention.
As far as I've investigated String.localeCompare() is not properly implemented in ExtendScript. On my platform it just seems locale-independent and simply performs the default UTF16 compare whatever the value of $.locale. Did anybody observe a different behavior?
@+
Marc
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more