【问题标题】:How sort string with spaces in javascript?如何在javascript中对带有空格的字符串进行排序?
【发布时间】:2021-08-04 10:34:53
【问题描述】:

我需要排序字符串。但是当它在字符串中找到空格时,它不会正确排序。我怎样才能让它不对空间进行排序?

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)

我希望看到:

[
{ attributes: { name: 'abc' } },
{ attributes: { name: 'abcd efg' } },
{ attributes: { name: 'äb' } },
{ attributes: { name: 'Ku cdf' } },
{ attributes: { name: 'Übd cd' } }
]

【问题讨论】:

  • 在转换为大写之前删除空格怎么样? name.replace(/\s+/g, '').toUpperCase()?
  • 预期结果是什么?
  • @evolutionxbox 删除空格但不影响排序。
  • @SalmanA 我编辑我的问题以查看。
  • 不确定你为什么在if 中使用localeCompare 的结果。这对于 1 和 -1 都是真实的。另外,你的第二个localeCompare,两者都有b.attributes

标签: javascript arrays sorting string-comparison


【解决方案1】:

String.localeCompare 方法返回一个数字,指示引用字符串是在排序顺序之前还是之后,或者与给定字符串相同...这与 Array.sort 应该返回的内容相同:

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);

【讨论】:

  • “de”是什么意思?
【解决方案2】:

localCompare 的工作方式是,如果第一个字符串较小,即在第二个字符串之前,它将返回一个负数。 如果第一个字符串更大,即它在第二个字符串之后,它将返回一个正数。

这一行:

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

即使第一个字符串更大或第二个字符串更大,也会为真。

问题是if (-1)if(any_negative_value) 被认为是true。即使localeCompare() 返回负值,您的第一个if 语句也将始终执行。第二个if 语句永远不会被执行。因此,无论a.attributes.name在字典上更大还是b.attributes.name更大,都会执行第一个if语句。

您不需要if 语句。 sort 函数只需要localCompare() 返回的数字即可。

因此,您可以简单地返回 localeCompare() 的值,它会正确地对属性进行排序。

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)

【讨论】:

    【解决方案3】:

    试试这个....

    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);

    【讨论】:

      【解决方案4】:

      您不需要删除空格。只需使用字符串的小写版本即可。

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

      【讨论】:

        猜你喜欢
        • 2015-12-05
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        • 2022-01-02
        • 2017-07-17
        相关资源
        最近更新 更多