Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

String localeCompare

Mentor ,
Aug 02, 2015 Aug 02, 2015

Hi every,

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

Does it need any library or preferences to work?

Jarek

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 02, 2015 Aug 02, 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

Translate
Community Expert ,
Aug 02, 2015 Aug 02, 2015

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

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Aug 02, 2015 Aug 02, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2015 Aug 02, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Aug 02, 2015 Aug 02, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Aug 03, 2015 Aug 03, 2015
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines