Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
Dart compareTo() sometimes returns different result than javascript localeCompare()

I'm looking for a javascript function that compares two strings in a "standard" way, or at least in the same way it does in dart.

I have this code in dart that give two strings it checks which is the largest and creates a single string separated by a "-" with the largest string in front.

//dart code
void main() {
  String currentUserId = "bbijdF7zKgR7jOIAs7Ka86jwdRu1";
  String peerId = "UVvUo4xCM4N4TtiFWL5B6vOFzN53";
  String groupChatId = "";
  if (currentUserId.compareTo(peerId) > 0) {
    groupChatId = '$currentUserId-$peerId';
  } else {
    groupChatId = '$peerId-$currentUserId';
  }
  print(groupChatId);
}

The print will return bbijdF7zKgR7jOIAs7Ka86jwdRu1-UVvUo4xCM4N4TtiFWL5B6vOFzN53

So far no problem.

Now I should replicate this code in javascript and I found the localeCompare link function which should do the same thing as compareTo() link in dart. But this is not the case since, in the case of the two strings, they have different results.

//js code
var currentUserId = "bbijdF7zKgR7jOIAs7Ka86jwdRu1";
var peerId = "UVvUo4xCM4N4TtiFWL5B6vOFzN53";
var groupChatId = "";
if (currentUserId.localeCompare(peerId) > 0) {
  groupChatId = `${currentUserId}-${peerId}`
} else {
  groupChatId = `${peerId}-${currentUserId}`
}
console.log(groupChatId)

The print will return UVvUo4xCM4N4TtiFWL5B6vOFzN53-bbijdF7zKgR7jOIAs7Ka86jwdRu1

There are rare cases in which they give two different results, but this is precisely the problem. Is there any function in javascript that behaves like the compareTo in dart?

If anyone could explain to me why these two functions sometimes differ in the result I would be very grateful

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In Dart, compareTo sorts the string by the character's code point but when using localeCompare the strings are compared in lexicographic order based on the user's current language. Additionally, the result from localeCompare may not be consistent between users with different locale settings.

A better way to compare your strings would be to add them to an array and .sort() them. This also orders by code point and will be consistent regardless of the user's locale setting. However, since you want the lower value string first you will need a custom sorting function to handle this.

Then, instead of using string literals to combine, just .join() the array with the - character.

let userId = "bbijdF7zKgR7jOIAs7Ka86jwdRu1";
let peerId = "UVvUo4xCM4N4TtiFWL5B6vOFzN53";

let chatId = [userId, peerId].sort(
  new Intl.Collator('en', { caseFirst: 'lower' }).compare
).join('-');

console.log(chatId);

This has proven to be surprisingly difficult. Hopefully this solution behaves as you expect in all circumstances.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!