custom-cards / flex-table-card

Highly Flexible Lovelace Card - arbitrary contents/columns/rows, regex matched, perfect to show appdaemon created content and anything breaking out of the entity_id + attributes concept
GNU General Public License v3.0
208 stars 26 forks source link

Sort only takes into consideration the first digit #6

Closed bbbenji closed 5 years ago

bbbenji commented 5 years ago

I am displaying a list of entities and their states and sorting by state. Whenever an entity hits 2 digits, it seems that this card only takes the first digit into account for sorting.

screenshot_20190218_182921

As soon as that first entity hit a state of 9, it popped to the bottom of the list.

config:

type: 'custom:flex-table-card'
sort_by: state+
title: ZTM
entities:
  include: sensor.ztm*
columns:
  - attr: friendly_name
    name: Number
  - prop: state
    name: Time
daringer commented 5 years ago

I believe this is a data-type issue. Means you have to make sure that the data is properly converted to a number. This depends on the origin of your data and how it is represented there.

To solve this you can make use of modify to parse the data to the designated data-type, in this case I would propose something like this:

...
columns:
  - prop: state
    name: Time
    modify: parseFloat(x)

Now sort_by should work as expected, take a look into issue #7 to see another similar example.

radical-squared commented 5 years ago

I can confirm parseFloat(x) solves the problem. Thanks for the hint.

radical-squared commented 5 years ago

I now have hard time getting proper number formatting with parseFloat(). Using .toFixed(3) results in bad sorting again. I tried adding and hiding a column for sorting purposes but then it turned out hidden: true does not work properly. It hides column title, but does not hide its values.

Nevertheless, I really appreciate your work. It's a really useful card that you've made.

daringer commented 5 years ago

oh no, this is obviously a bug, but can likely be easy fixed, let's see if I can solve this this evening. Despite that if you would like to use toFixed() the number is coverted to a string again, thus not numerically sortable, other sorting mechanisms (i.e., table viewer tools, fileexplorer) usually implement a "natural" sorting instead of the formal sorting applied here, this might be a solution also to avoid this whole hassle.

In the meantime the best way would really be the hidden flag, looks like I broke during the recent feature-hunt commits, but should be easy... will keep you updated....

daringer commented 5 years ago

2 lines fix, couldn't resist. thanks for pointing this out, hopefully I did not introduce a new bug, but hidden: true at least will hide the header and the contents again.

radical-squared commented 5 years ago

Now it’s perfect. Glad it was a simple fix. Thanks again!

daringer commented 5 years ago

Then I'll close this issue, don't hesitate to find new bugs :D And if you're building a nice table and you think it would be good as an example, feel free to share...