How do you sort an array on multiple columns?

前端 未结 16 1110
面向向阳花
面向向阳花 2020-11-22 13:02

I have a multidimensional array. The primary array is an array of

[publicationID][publication_name][ownderID][owner_name] 

What I am tryin

16条回答
  •  名媛妹妹
    2020-11-22 13:36

    My own library for working with ES6 iterables (blinq) allows (among other things) easy multi-level sorting

    const blinq = window.blinq.blinq
    // or import { blinq } from 'blinq'
    // or const { blinq } = require('blinq')
    const dates = [{
        day: 1, month: 10, year: 2000
      },
      {
        day: 1, month: 1, year: 2000
      },
      {
        day: 2, month: 1, year: 2000
      },
      {
        day: 1, month: 1, year: 1999
      },
      {
        day: 1, month: 1, year: 2000
      }
    ]
    const sortedDates = blinq(dates)
      .orderBy(x => x.year)
      .thenBy(x => x.month)
      .thenBy(x => x.day);
    
    console.log(sortedDates.toArray())
    // or console.log([...sortedDates])

提交回复
热议问题