首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在javascript中对包含数字、字母以及日期和时间的字符串进行自然排序

在javascript中对包含数字、字母以及日期和时间的字符串进行自然排序
EN

Stack Overflow用户
提问于 2018-06-28 03:35:07
回答 1查看 79关注 0票数 2

下面是我想要排序的数组:

let documentData=[
{'title':'01 Documents >File0010-Donia5,06/14/2018,1.14.03 PM.pdf'},
{'title':'01 Documents >File0010-Donia5,06/14/2018,5.14.03 AM.pdf'},
{'title':'04 Images > Image0010-image59323.jpg'},
{'title':'04 Images > Image0010-image44005.jpg'},
{'title':'01 Documents >File0010-Donia5,08/04/2018,5.14.03 PM.pdf'},
{'title':'01 Documents >File0010-Donia5,12/14/2018,10.14.03 AM.pdf'},
    ];

下面是我用来运行自然排序的代码。

console.log(documentData.sort((a,b)=>
      a.title.toLowerCase().replace(/\>| |\-/g,'')
        .localeCompare(
            b.title.toLowerCase().replace(/\>| |\-/g,''),
            undefined,{numeric:true, sensitivity:'base'})));

下面是我得到的输出

[ { title: '01 Documents >File0010-Donia5,06/14/2018,1.14.03 PM.pdf' },
  { title: '01 Documents >File0010-Donia5,06/14/2018,5.14.03 AM.pdf' },
  { title: '01 Documents >File0010-Donia5,08/04/2018,5.14.03 PM.pdf' },
  { title: '01 Documents >File0010-Donia5,12/14/2018,10.14.03 AM.pdf' },
  { title: '04 Images > Image0010-image44005.jpg' },
  { title: '04 Images > Image0010-image59323.jpg' } ]

这不是我需要的排序。如果每个字符串除了数字和字母之外还包含日期/时间,我如何正确地对字符串数组进行排序?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51070208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档