withDatesDescending

withDatesDescending

  • (left: date, right: date) => number
  1. A compare function which orders dates from latest to earliest.

    const dates = [
      new Date('2025 03 15'),
      new Date('2025 03 14'),
      new Date('2025 03 16'),
    ]
    const updatedDates = dates.toSorted(withDatesDescending)
    console.log(updatedDates)
    // is [
    //   2025 03 16
    //   2025 03 15
    //   2025 03 14
    // ]
    const dates = [
      new Date('2025 03 15'),
      new Date('2025 03 14'),
      new Date('2025 03 16'),
    ]
    const updatedDates = dates.toSorted(withDatesDescending)
    console.log(updatedDates)
    // is [
    //   2025 03 16
    //   2025 03 15
    //   2025 03 14
    // ]
    
    const dates = [
      new Date('2025 03 15'),
      new Date('2025 03 14'),
      new Date('2025 03 16'),
    ]
    const updatedDates = dates.toSorted(
      withDatesDescending
    )
    console.log(updatedDates)
    // is [
    //   2025 03 16
    //   2025 03 15
    //   2025 03 14
    // ]
    const dates = [
      new Date('2025 03 15'),
      new Date('2025 03 14'),
      new Date('2025 03 16'),
    ]
    const updatedDates = dates.toSorted(
      withDatesDescending
    )
    console.log(updatedDates)
    // is [
    //   2025 03 16
    //   2025 03 15
    //   2025 03 14
    // ]
    
  2. We often need to order by date. Below, we have some social media comments. Let's order them with the most recent showing first.

    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 byPostedDescending = compareByProp('posted', withDatesDescending)
    const orderComments = order(byPostedDescending)
    const udpatedComments = orderComments(comments)
    console.log(udpatedComments)
    // is [
    //   {
    //     text: Wish I was there!
    //     upvotes: 8
    //     posted: 2025 03 17
    //   }
    //   {
    //     text: :)
    //     upvotes: 2
    //     posted: 2025 03 16
    //   }
    //   {
    //     text: Looks like a fun time by the lake
    //     upvotes: 5
    //     posted: 2025 03 15
    //   }
    // ]
    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 byPostedDescending = compareByProp('posted', withDatesDescending)<Comment>
    const orderComments = order(byPostedDescending)
    const udpatedComments = orderComments(comments)
    console.log(udpatedComments)
    // is [
    //   {
    //     text: Wish I was there!
    //     upvotes: 8
    //     posted: 2025 03 17
    //   }
    //   {
    //     text: :)
    //     upvotes: 2
    //     posted: 2025 03 16
    //   }
    //   {
    //     text: Looks like a fun time by the lake
    //     upvotes: 5
    //     posted: 2025 03 15
    //   }
    // ]
    
    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 byPostedDescending = compareByProp(
      'posted',
      withDatesDescending
    )
    const orderComments = order(byPostedDescending)
    const udpatedComments = orderComments(comments)
    console.log(udpatedComments)
    // is [
    //   {
    //     text: Wish I was there!
    //     upvotes: 8
    //     posted: 2025 03 17
    //   }
    //   {
    //     text: :)
    //     upvotes: 2
    //     posted: 2025 03 16
    //   }
    //   {
    //     text: Looks like a fun time by the lake
    //     upvotes: 5
    //     posted: 2025 03 15
    //   }
    // ]
    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 byPostedDescending = compareByProp(
      'posted',
      withDatesDescending
    )<Comment>
    const orderComments = order(byPostedDescending)
    const udpatedComments = orderComments(comments)
    console.log(udpatedComments)
    // is [
    //   {
    //     text: Wish I was there!
    //     upvotes: 8
    //     posted: 2025 03 17
    //   }
    //   {
    //     text: :)
    //     upvotes: 2
    //     posted: 2025 03 16
    //   }
    //   {
    //     text: Looks like a fun time by the lake
    //     upvotes: 5
    //     posted: 2025 03 15
    //   }
    // ]