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

214
Views
How sort string with spaces in javascript?

I need sort strings. But it doesn't sort correctly when it finds spaces in string. How can I make it not to sort spaces?

const array = [
        { attributes: { name: 'abcd efg' } },
        { attributes: { name: 'Übd cd' } },
        { attributes: { name: 'Ku cdf' } },
        { attributes: { name: 'äb' } },
        { attributes: { name: 'abc' } }
      ]
      array.sort((a, b) => {
        if (a.attributes.name.toUpperCase()
          .localeCompare(b.attributes.name.toUpperCase(), 'de', { sensitivity: 'base' })) { return -1 }
        if (b.attributes.name.toUpperCase()
          .localeCompare(b.attributes.name.toUpperCase(), 'de', { sensitivity: 'base' })) { return 1 }
        return 0
      })
console.log('typeof array', array)

I expect to see:

[
{ attributes: { name: 'abc' } },
{ attributes: { name: 'abcd efg' } },
{ attributes: { name: 'äb' } },
{ attributes: { name: 'Ku cdf' } },
{ attributes: { name: 'Übd cd' } }
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The String.localeCompare method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order... which is the same as what Array.sort is supposed to return:

const array = [
  { attributes: { name: "abcd efg" } },
  { attributes: { name: "Übd cd" } },
  { attributes: { name: "Ku cdf" } },
  { attributes: { name: "ab" } }
];
array.sort((a, b) => a.attributes.name.toUpperCase().localeCompare(b.attributes.name.toUpperCase(), "de", { sensitivity: "base" }));
console.log(array);

about 4 years ago · Juan Pablo Isaza Report

0

The way localCompare works is, if the first string is smaller i.e. comes before the second string, it will return a negative number. And if the first string is greater i.e. it comes after the second string, it will return a positive number.

This line:

if (a.attributes.name.toUpperCase()
          .localeCompare(b.attributes.name.toUpperCase(), 'de', { sensitivity: 'base' })) { return -1 }

will be true even if the first string is greater or the second string is.

The problem is that if (-1) or if(any_negative_value) is considered true. Even if localeCompare() returns a negative value, your first if statement will always execute. The second if statement will never be executed. Therefore, no matter if the a.attributes.name is lexicographically greater or b.attributes.name is greater, the first if statement will always be executed.

You do not need the if statements. The sort function just needs the number returned by localCompare().

Hence, you can simply return the value of localeCompare() and it will sort the attributes correctly.

const array = [
        { attributes: { name: 'abcd efg' } },
        { attributes: { name: 'Übd cd' } },
        { attributes: { name: 'Ku cdf' } },
        { attributes: { name: 'ab' } }
]
      array.sort(
        (a, b) => a
          .attributes
          .name
          .toUpperCase()
          .localeCompare(b
            .attributes
            .name
            .toUpperCase(), 
            'de', 
            { sensitivity: 'base' }
          )
        );
console.log('typeof array', array)

about 4 years ago · Juan Pablo Isaza Report

0

Try this one....

var hasLeading = s => /^\S+\s\S+\s\S+$/.test(s);
var array = [
    { attributes: { name: 'abcd efg' } },
    { attributes: { name: 'Übd cd' } },
    { attributes: { name: 'Ku cdf' } },
    { attributes: { name: 'ab' } }
];

array.sort((a, b) => hasLeading(b.attributes.name.toUpperCase()) - hasLeading(a.attributes.name.toUpperCase()) || a.attributes.name.toUpperCase() > b.attributes.name.toUpperCase() || -(a.attributes.name.toUpperCase() < b.attributes.name.toUpperCase())
);

console.log(array);

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!