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

推荐订阅源

C
Cyber Attacks, Cyber Crime and Cyber Security
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
W
WeLiveSecurity
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
News | PayPal Newsroom
D
DataBreaches.Net
博客园_首页
Y
Y Combinator Blog
F
Fortinet All Blogs
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
Help Net Security
Help Net Security
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
V
Vulnerabilities – Threatpost
A
About on SuperTechFans
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
L
LINUX DO - 热门话题
Simon Willison's Weblog
Simon Willison's Weblog
雷峰网
雷峰网
G
Google Developers Blog
Cyberwarzone
Cyberwarzone
I
Intezer
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
C
Check Point Blog
AI
AI
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
博客园 - 司徒正美

博客园 - 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, 禁止转载 🈲️,侵权必究⚠️!