withDatesAscending
withDatesAscending
(left: date, right: date) => number
A compare function which orders dates from earliest to latest.
const dates = [ new Date('2025 03 15'), new Date('2025 03 14'), new Date('2025 03 16'), ] const updatedDates = dates.toSorted(withDatesAscending) console.log(updatedDates) // is [ // 2025 03 14 // 2025 03 15 // 2025 03 16 // ]
const dates = [ new Date('2025 03 15'), new Date('2025 03 14'), new Date('2025 03 16'), ] const updatedDates = dates.toSorted(withDatesAscending) console.log(updatedDates) // is [ // 2025 03 14 // 2025 03 15 // 2025 03 16 // ]
const dates = [ new Date('2025 03 15'), new Date('2025 03 14'), new Date('2025 03 16'), ] const updatedDates = dates.toSorted( withDatesAscending ) console.log(updatedDates) // is [ // 2025 03 14 // 2025 03 15 // 2025 03 16 // ]
const dates = [ new Date('2025 03 15'), new Date('2025 03 14'), new Date('2025 03 16'), ] const updatedDates = dates.toSorted( withDatesAscending ) console.log(updatedDates) // is [ // 2025 03 14 // 2025 03 15 // 2025 03 16 // ]
We often need to order by date. Below, we have some social media comments. Let's order them by the posted date.
const comments = [ { text: 'Looks like a fun time by the lake', upvotes: 5, posted: new Date('2025 03 15'), }, { text: 'Wish I was there!', upvotes: 8, posted: new Date('2025 03 17'), }, { text: ':)', upvotes: 2, posted: new Date('2025 03 16'), }, ] const byPostedAscending = compareByProp('posted', withDatesAscending) const orderComments = order(byPostedAscending) const udpatedComments = orderComments(comments) console.log(udpatedComments) // is [ // { // text: Looks like a fun time by the lake // upvotes: 5 // posted: 2025 03 15 // } // { // text: :) // upvotes: 2 // posted: 2025 03 16 // } // { // text: Wish I was there! // upvotes: 8 // posted: 2025 03 17 // } // ]
type Comment = { text: string upvotes: number posted: Date } const comments: Comment[] = [ { text: 'Looks like a fun time by the lake', upvotes: 5, posted: new Date('2025 03 15'), }, { text: 'Wish I was there!', upvotes: 8, posted: new Date('2025 03 17'), }, { text: ':)', upvotes: 2, posted: new Date('2025 03 16'), }, ] const byPostedAscending = compareByProp('posted', withDatesAscending)<Comment> const orderComments = order(byPostedAscending) const udpatedComments = orderComments(comments) console.log(udpatedComments) // is [ // { // text: Looks like a fun time by the lake // upvotes: 5 // posted: 2025 03 15 // } // { // text: :) // upvotes: 2 // posted: 2025 03 16 // } // { // text: Wish I was there! // upvotes: 8 // posted: 2025 03 17 // } // ]
const comments = [ { text: 'Looks like a fun time by the lake', upvotes: 5, posted: new Date('2025 03 15'), }, { text: 'Wish I was there!', upvotes: 8, posted: new Date('2025 03 17'), }, { text: ':)', upvotes: 2, posted: new Date('2025 03 16'), }, ] const byPostedAscending = compareByProp( 'posted', withDatesAscending ) const orderComments = order(byPostedAscending) const udpatedComments = orderComments(comments) console.log(udpatedComments) // is [ // { // text: Looks like a fun time by the lake // upvotes: 5 // posted: 2025 03 15 // } // { // text: :) // upvotes: 2 // posted: 2025 03 16 // } // { // text: Wish I was there! // upvotes: 8 // posted: 2025 03 17 // } // ]
type Comment = { text: string upvotes: number posted: Date } const comments: Comment[] = [ { text: 'Looks like a fun time by the lake', upvotes: 5, posted: new Date('2025 03 15'), }, { text: 'Wish I was there!', upvotes: 8, posted: new Date('2025 03 17'), }, { text: ':)', upvotes: 2, posted: new Date('2025 03 16'), }, ] const byPostedAscending = compareByProp( 'posted', withDatesAscending )<Comment> const orderComments = order(byPostedAscending) const udpatedComments = orderComments(comments) console.log(udpatedComments) // is [ // { // text: Looks like a fun time by the lake // upvotes: 5 // posted: 2025 03 15 // } // { // text: :) // upvotes: 2 // posted: 2025 03 16 // } // { // text: Wish I was there! // upvotes: 8 // posted: 2025 03 17 // } // ]