0

I've a problem with a sorting JS plugin for my tables. It's named "Sortable". I've did everything described in the documentation to setup everything. The sorting works but there is an issue with dates and prices.

So when I sort for example a date column ascending, the rows looking like this:

  1. 22.10.2018
  2. 23.02.2019
  3. 28.02.2019
  4. 28.12.2018

You can see the date config here:

https://github.hubspot.com/sortable/api/options/

Maybe it's a problem with the german date format but I don't know exactly how to fix this. Maybe you can take a look at it? This would be really awesome!

1 Answer 1

1

It's sorted in alphabetic order. because german date format can't be parsed by javascript Date.parse. You should add custom type.

Here’s how Sortable internally sets up the defaults.

sortable.setupTypes [{
  name: 'numeric'
  defaultSortDirection: 'descending'
  match: (a) -> a.match numberRegExp
  comparator: (a) -> parseFloat(a.replace(/[^0-9.-]/g, ''), 10) or 0
}, {
  name: 'date'
  defaultSortDirection: 'ascending'
  reverse: true
  match: (a) -> not isNaN Date.parse a
  comparator: (a) -> Date.parse(a) or 0
}, {
  name: 'alpha'
  defaultSortDirection: 'ascending'
  match: -> true
  compare: (a, b) -> a.localeCompare b
}]

note match and comparator of date type. change those to:

  match: (a) -> not isNaN Date.parse a.split('.').reverse().join('.')
  comparator: (a) -> Date.parse(a.split('.').reverse().join('.')) or 0

and add these after sortable.init() call.

By the way, this is coffeescript. so use it accordingly.

Sign up to request clarification or add additional context in comments.

5 Comments

This is what I've tried. But it's still sorting not correctly. Maybe because of the german date format?
yes it's because of the german date format. Date.parse('22.10.2018') is NaN. date type implementation use this code. You can set custom type for this
How do I need to modify this to reach my goal?
I changed my answer. please check
Awesome mate. Works great, thanks! Do you want to help me with another filter? It's about filtering prices but there is a bit of a problem. I would describe this in a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.