nicolaskruchten / pivottable

Open-source Javascript Pivot Table (aka Pivot Grid, Pivot Chart, Cross-Tab) implementation with drag'n'drop.
https://pivottable.js.org/
MIT License
4.36k stars 1.08k forks source link

Sorting Date in column #1008

Open mortoza opened 6 years ago

mortoza commented 6 years ago

Hi Nicolas, I have been using your beautiful pivotTable component in CUBA PLATFORM which is awesome!

I have a question if you do not mind. I want to sort date which in Column. The date is dynamic value coming from datasource and it is currently not ordered in the pivotTable when loaded. As an workaround, I can use the controller (arrow) on the pivotTable to sort manually but the date order is only ordered when it is within the same month.

I have seen an example to compare dates but not sure if that will work here! Do you have any suggestion?

nicolaskruchten commented 6 years ago

You can use the sorters parameter to provide your own comparator to override the default sorting, which is alphabetical.

guastallaigor commented 6 years ago

Just to share, I made an example on how to sort dates. Note that I received a date in the format dd/mm/aaaa (pt-BR).

...
cols: [],
sorters: {
    "DATE": function(a, b) {
      const [di, me, an] = a.split('/');
      const [di2, me2, an2] = b.split('/');
      var da = new Date(`${an}-${me}-${di}`).getTime();
      var db = new Date(`${an2}-${me2}-${di2}`).getTime();

      return da < db ? -1 : da > db ? 1 : 0
  }
},
...