Skip to main content
Jump_Over
Legend
August 2, 2015
Answered

String localeCompare

  • August 2, 2015
  • 1 reply
  • 1357 views

Hi every,

Has anyone experience with StringA.localeCompare(StringB) method?

Does it need any library or preferences to work?

Jarek

This topic has been closed for replies.
Correct answer Peter Kahrel

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

1 reply

Peter Kahrel
Community Expert
Community Expert
August 2, 2015

You don't need libraries, it's used to compare plain strings. See here: String.prototype.localeCompare() - JavaScript | MDN

Peter

Jump_Over
Jump_OverAuthor
Legend
August 2, 2015

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

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
August 2, 2015

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