Bootstrap-table: Sorting Date Data type

Created on 12 Aug 2014  路  12Comments  路  Source: wenzhixin/bootstrap-table

How to sort the Date in ascending and descending order. We are only able to sort depending on the alphabets. It is a question rather that an issue.

Most helpful comment

Doesn't seem like anyone is offering up their code, so here's mine:

Step 1. Add this attribute to the tag: data-sorter="dateSorter"

Step 2. Add this to any script block:

function dateSorter(a, b) {
  var d1 = new Date(a);
  var d2 = new Date(b);

  if (d1 < d2) return -1;
  if (d1 > d2) return 1;
  return 0;
}

Cheers!

All 12 comments

You can use sorter option.
for example: https://examples.bootstrap-table.com/#column-options/sorter.html

Thank u Wenzhixin, it worked like as charm :)

You're welcome.

If this plugin is useful to you, you can make me feel happy by starting the project :)

zubair2irfan, Any chance you could share the JS you wrote to accomplish this? Thanks in advance.

Doesn't seem like anyone is offering up their code, so here's mine:

Step 1. Add this attribute to the tag: data-sorter="dateSorter"

Step 2. Add this to any script block:

function dateSorter(a, b) {
  var d1 = new Date(a);
  var d2 = new Date(b);

  if (d1 < d2) return -1;
  if (d1 > d2) return 1;
  return 0;
}

Cheers!

Doesn't seem like anyone is offering up their code, so here's mine:

Step 1. Add this attribute to the tag: data-sorter="dateSorter"

Step 2. Add this to any script block:

function dateSorter(a, b) {
  var d1 = new Date(a);
  var d2 = new Date(b);

  if (d1 < d2) return -1;
  if (d1 > d2) return 1;
  return 0;
}

Cheers!

I added this inside tag having date picker and JS too, i want to do it based on desc order.

You're welcome.

can u please share it again , i'm trying to access the link http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#custom-sort-table but received 404 error

You can use sorter option.
for example: http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#custom-sort-table

this link doesn't work

https://bootstrap-table.com/docs/api/column-options/#sorter

thanks , but i'm trying to sort the date time column in bootstrap table. i tried using sorter but still no luck

Please provide a jsFiddle to show your problem, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ME-at-GH picture ME-at-GH  路  3Comments

iamthestreets picture iamthestreets  路  5Comments

sbellani picture sbellani  路  4Comments

Sivalingaamorthy picture Sivalingaamorthy  路  5Comments

startovernow picture startovernow  路  4Comments