Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda