cancel
Showing results for 
Search instead for 
Did you mean: 

TestComplete Javascript localeCompare doesn't work

tphillips
Community Leader

TestComplete Javascript localeCompare doesn't work

I am trying to sort an array with strings with numbers in them, and I am wanting it to be sorted "naturally", i.e. "test1", "test10", "test2" => "test1", "test2", "test10"

I see that we should be able to use the localeCompare() function to do this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompar...

However, the following code prints 1 ("2" > "10) for each statement:

 

function test2() {
    Log.Message("2".localeCompare("10")); // Prints 1 ("2" > "10")
    Log.Message("2".localeCompare("10", undefined, {numeric: true})); // Should print -1 (2 < 10) but prints 1
}

 

tphillips_1-1620181981016.png

Am I doing something wrong?

Is there another way to do natural sorting, maybe using aqString functions?

 

In the meantime I have written my own compare function that extracts the numeric parts, converts them to ints, and compares them. I shouldn't have to do this, though.

 


--------------------
QA Systems Architect
New Here?
Join us and watch the welcome video:
Announcements