惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

博客园 - anonymous007

博文阅读密码验证 - 博客园 GraphQL.js All In One 2022 FIFA 世界杯决赛对阵组件库 All In One 2022 FIFA 世界杯 首发阵容组件库 All In One js 字符串转数组,数组解构默认值 All In One 《走遍美国》经典英语学习视频 All In One Error: EISDIR: illegal operation on a directory, read sheep match disappear game All In One 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 nrm & npm config All In One node.js & npm version not match bug All In One Next.js 13 All In One 博文阅读密码验证 - 博客园 Yarn 2.x 升级迁移指南 All In One MongoDB Database Tools All In One 支付宝小程序上线发布流程 All In One 微信小程序上线发布流程 All In One 博文阅读密码验证 - 博客园
js sort array by date string All In One
anonymous007 · 2022-11-30 · via 博客园 - anonymous007

js sort array by date string All In One

bug


const log = console.log;

const arr = [
  { title: 'markdown title', date: '2021-01-01' },
  { title: 'markdown title', date: '2022-11-01' },
  { title: 'markdown title', date: '2022-11-30' },
  { title: 'markdown title', date: '2022-12-01' },
  { title: 'markdown title', date: '2022-12-31' },
  { title: '✅ markdown title', date: '2012-01-01' }
];

// date string sort bug ❌
const sorted = arr.sort(({ date: a }, { date: b }) => {
  if (a < b) {
    return 1;
  } else if (a > b) {
    return -1;
  } else {
    return 0;
  }
});


image

solution


const log = console.log;

const arr = [
  { title: 'markdown title', date: '2021-01-01' },
  { title: 'markdown title', date: '2022-11-01' },
  { title: 'markdown title', date: '2022-11-30' },
  { title: 'markdown title', date: '2022-12-01' },
  { title: 'markdown title', date: '2022-12-31' },
  { title: '✅ markdown title', date: '2012-01-01' }
];

// const meta = arr.map(obj => obj.metadata);
// log(`meta =`, meta);
// return arr;
// const sorted = arr.sort(({ date: a }, { date: b }) => {
//   // 升序排序,最旧的在最前面 ❌
//   if (a < b) {
//     return -1;
//   } else if (a > b) {
//     return 1;
//   } else {
//     return 0;
//   }
// });
const sorted = arr.sort(({ date: a }, { date: b }) => {
  // 降序排序,最新的在最前面 ✅
  if (a < b) {
    return 1;
  } else if (a > b) {
    return -1;
  } else {
    return 0;
  }
});
log(`sorted =`, sorted.map(obj => obj.metadata));
return sorted;

image

Date.prototype.setHours()

//

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

refs

String => Date

https://masteringjs.io/tutorials/fundamentals/sort-by-date

https://github.com/mastering-js/masteringjs.io

https://www.delftstack.com/howto/javascript/sort-by-date-javascript/

https://thewebdev.info/2022/01/23/how-to-sort-a-date-string-array-with-javascript/


Flag Counter


©xgqfrms 2012-2020

www.cnblogs.com/anonymous007 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️anonymous007, 禁止转载 🈲️,侵权必究⚠️!