2

I wish to compare two strings in javascript. I am using localeCompare method but the output is not as expected

116457 < 3085
false
"116457" < "3085"
true
"116457".localeCompare("3085")
-1

Output in second and third case is not as expected.

I know it sorts in Lexicographical order but still I am having trouble understanding why is it so and how should I overcome this.

Any help would be appreciated.

ponury-kostek
8,0835 gold badges27 silver badges33 bronze badges
asked Jul 11, 2019 at 11:57
6
  • 2
    "1" comes before "3", so any String starting with "1" comes before any other string starting with "3". You say you know this but have trouble understanding why. Would you agree that "a" < "c"? Commented Jul 11, 2019 at 11:59
  • Those strings are always integers ? If yes, why won't you cast it using parseInt(str) ? Commented Jul 11, 2019 at 12:01
  • Actually no. In the scenario I am handling, they won't always be integers Commented Jul 11, 2019 at 12:03
  • 1
    re "how should I overcome this": if you want suggestions for solutions, you'll have to be a lot more specific on what the requirements are. What pattern do your inputs have, and how do you want them sorted? You said "c-3085" can occur; can "a-5" and "b-1" and "a-b+7-c+1" occur too, and how should they sort? Commented Jul 11, 2019 at 12:14
  • Thanks everyone for the help. I will be writing my own function! Commented Jul 12, 2019 at 17:41

2 Answers 2

4

If you want to compare them without converting them to numbers, you can set numeric: true in the options parameter

console.log(
 "116457".localeCompare("3085", undefined, { numeric: true })
)
console.log(
 "116457".localeCompare("3085")
)

answered Jul 11, 2019 at 12:10
Sign up to request clarification or add additional context in comments.

Comments

4

If 116457 were a word, it would come before 3085 in a dictionary.

Consider a dictionary with

  • "applicative" (a long word starting with "a", c.f. a long digit string starting with "1")
  • "copy" (a short word starting with "c", c.f. a shorter digit string starting with "3").
answered Jul 11, 2019 at 11:59

8 Comments

Okay. Makes sense.
But how do I overcome this? (I don't want to convert it to a number)
Convert it to a number? "I don't want to" ... and why?
I basically "can't" cast it using parseInt(str) or Number(str) as in the scenario I am handling, it doesn't always gets integers. There are cases I get strings as well. Example : "c-116457" and "c-3085"
Well, if you have complex strings where various substrings have specific meaning, then you'll have to write your own comparator function that takes all of that into acount. It may be more efficient to represent your data differently, e.g. as objects with a bunch of properties rather than as a string.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.