Author Topic: Sorting a String with decimal comma by handling it as float  (Read 2830 times)

pquser

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
Sorting a String with decimal comma by handling it as float
« on: September 15, 2017, 04:08:50 pm »
Hi,

in my table there are values like "2,5 pieces", "4,00 pieces" and such.

I want to set up my colModel in order to handle this string values as float and sort them in the correct order.
If I just set "dataType" to "float" they are not sortable any more.
Guess I need some javascript magic in here.

Any Idea how to do this?
Thanks.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Sorting a String with decimal comma by handling it as float
« Reply #1 on: September 16, 2017, 09:08:29 am »
values like "2,5 pieces", "4,00 pieces" can't be sorted like floats.

Workaround is to use formula to extract float values form strings using a regular expression and store in a separate column.

Let's say column having values like "2,5 pieces", "4,00 pieces" has dataIndx = 'a'.

Add another column with dataIndx = 'b'

use row formula to extract float values from column a and save in column b.

Keep column a as hidden and column b visible.

use render function in column b to display corresponding string values from column a.

pquser

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Sorting a String with decimal comma by handling it as float
« Reply #2 on: September 16, 2017, 06:25:00 pm »
No need for such a hack as I can change the SQL query to deliver "pieces" as an extra column.

If I do so, will setting the type to "float" work with a comma as decimal separator (German) instead of a dot? Or will I need to do some adjustment javascript wise?

Thanks.

pquser

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Sorting a String with decimal comma by handling it as float
« Reply #3 on: September 16, 2017, 11:29:42 pm »
If I do so, will setting the type to "float" work with a comma as decimal separator (German) instead of a dot? Or will I need to do some adjustment javascript wise?

Seems it doesn't. I can't sort fields with decimal comma any more when set to "float".

Any hint how to fix this?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Sorting a String with decimal comma by handling it as float
« Reply #4 on: September 18, 2017, 07:09:21 am »
As you have access to data source/ sql query, it's quite simple.

1.return the data as true float values as 1234.56 which would ensure correct sorting.

2 and use column.format = "#.###,00 pieces" to display it in the format you wish to your users.