Forum Discussion

tphillips's avatar
tphillips
Frequent Contributor
4 years ago

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/localeCompare#numeric_sorting

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
}

 

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.

 

No RepliesBe the first to reply