Skip to main content
johnrellis
Legend
April 19, 2017

P: Lightroom SDK: LrStringUtils.compareStrings() doesn't compare strings with digits correctly

  • April 19, 2017
  • 7 replies
  • 475 views

LrStringUtils.compareStrings() doesn't compare strings containing digits correctly.  For example, it says that "a9b" comes before "a10b":



It appears that compareStrings() is comparing sequences of digits numerically, e.g. as the numbers 9 and 10 in the example above.   While that definition can be useful in some cases, such as ordering filenames containing sequence numbers, it is not generally useful for sorting.  

The SDK should restore the traditional meaning to compareStrings() and optionally provide a second compare function for the current functionality.

This topic has been closed for replies.

7 replies

Sunil_Bhaskaran
Inspiring
March 9, 2018
Thanks, John. We will revisit the documentation.

Thanks,
Sunil
johnrellis
Legend
March 8, 2018

Prior to LR 7.2, LrStringUtils.compareStrings() compared numbers embedded in the middle of strings as numbers, not as strings, e.g. "a9x" < "a10x", similar to how Mac Finder and Windows File Explorer sort filenames.  LR 7.2 enhances that functionality to provide traditional string compare:

Lightroom SDK: LrStringUtils.compareStrings() doesn't compare strings with digits correctly | Photoshop Family Customer …

Absent updated SDK documentation, some reverse engineering suggests the following new definition of compareStrings() in LR 7.2:

LrStringUtils.compareStrings (string x, string y [, boolean strictAlphanumeric])

Returns true iff the string "x" precedes the string "y" in the user's locale. 

If "strictAlphanumeric" is true, the strings are compared using normal alphanumeric comparisons.

If "strictAlphanumeric" is false (the default), sequences of digits within the strings are interpreted as numbers and compared as numbers rather than strings, similar to how Mac Finder and Windows File Explorer sort filenames. For example, "a9x" < "a10x".

More precisely, the strings "x" and "y" are parsed into maximal sequences of non-digits and digits, and the corresponding sequences compared in turn.  If corresponding sequences both contain digits only, they are compared as numbers, not strings; otherwise, they are compared as strings.

johnrellis
Legend
March 8, 2018
Absent updated SDK documentation, some reverse engineering suggests the following new definition of compareStrings() in LR 7.2:

LrStringUtils.compareStrings (string x, string y [, boolean strictAlphanumeric])

Returns true iff the string "x" precedes the string "y" in the user's locale. 

If "strictAlphanumeric" is true, the strings are compared using normal alphanumeric comparisons.

If "strictAlphanumeric" is false (the default), sequences of digits within the strings are interpreted as numbers and compared as numbers rather than strings, similar to how Mac Finder and Windows File Explorer sort filenames. For example, "a9x" < "a10x". 

More precisely, the strings "x" and "y" are parsed into maximal sequences of non-digits and digits, and the corresponding sequences compared in turn.  If corresponding sequences both contain digits only, they are compared as numbers, not strings; otherwise, they are compared as strings.
axmelissarios1
Legend
February 13, 2018
Hi all,

This issue should be fixed in today's Lightroom Classic CC 7.2 version. Please update your application and let us know if you still see this issue!

Lightroom Classic CC 7.2 is now available!

Thanks,
Melissa
Melissa Rios, Product Manager, Community Experiences &amp; Platforms | Adobe
johnrellis
Legend
April 19, 2017
Thanks.
abhishek_india
Participating Frequently
April 19, 2017
Currently LrStringUtils provides only single comparison function to compare 2 strings, which does numeric string search.
E.g.,, image_7 < image_15.
This is because all the filenames or timing data follow this natural order. So, for practical scenarios, this function should suffice. image_7 is supposed to come before image_15 in sorted order.

But, yes the SDK should provide a comparison function similar to C's strcmp().
We'll look into it. And also try to describe the APIs in documentation a bit more.

Thanks for reporting.

Thanks,
Abhishek
Sunil_Bhaskaran
Inspiring
April 19, 2017
Thanks for reporting this.
Let us look into it.

Thanks,
Sunil