Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

307
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda