Sorting issue

Sorting issue

Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0

Link to test case:
https://jsfiddle.net/z5rnv7mh/8/
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Hi, Above I have attached my demo grid.
I am unable to sort my grid although gird sorting was working fine and still having issues in the sorting of the last column *Todays Gain/Loss" in the grid.

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited April 2022

    You are getting this error:

    Uncaught TypeError: Cannot read properties of undefined (reading 'replace')

    Which is caused by this statement:

    var d = data.split(' ')[4].replace(/,/g,"");
    

    That suggests the [4] array index is out of bounds. Not sure what you goal is but you should validate the result of data.split(' ') before trying to access the resulting array.

    The error is causing the Datatables functions to no work.

    Kevin

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0
    edited April 2022

    Can you plz fix the problem in JS fiddle file, actually the last column of the grid is not sortable, It's not sorting properly grid data?
    I did the exact work on other places its working fine.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    As I said I don't know what you are trying to do with the above statements. I commented them out here and sorting works:
    https://jsfiddle.net/j2du3ga6/

    If any of the columns isn't sorting as you want then tell us how you expect it to sort.

    Kevin

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0
    edited April 2022

    https://jsfiddle.net/osqaLc92/2/
    Here is the URL of my DT grid. The last 3 columns are not sorted properly.

    In the columns, there are mixed data, strings, numbers, and special characters, and also some negative values are there in the grid as well.

    But Datatable default sorting is not working.

    Can you plz check it out and guide me to the solution or fix the issue of the sorting.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You're going along the right path with columns.render, you just need to determine what you want to sort by. The "Cash" column appears to work, but the others are returning values like "(2.15%)" which would be treated like a string,

    Colin

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0

    So can you plz set a demo example? So that would be easy for me to understand.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    You have data like this in the last two columns: 21,457 (2.15%). As I asked before what do you want to sort by? Its just a matter of using Javascript tools to parse the string the return the data you want sort by in this block of code:

          if (type === 'sort' || type === 'type') {
            var d = data.split(' ')[4].replace(/,/g, "");
            console.log(d)
            return d;
          }
    

    Kevin

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0

    I want to sort in Ascending and descending order, positive and negative values as well. The problem I am facing right now last column of the grid is not sorting orderly.
    In negative values its showing the wrong sorting order.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The question is you have two numbers 21,457 and 2.15% in those columns. Which do you want to sort by?

    Kevin

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0
    edited April 2022

    I want to sort From 1st number 21,457.

  • Uzairkhan92Uzairkhan92 Posts: 36Questions: 10Answers: 0

    I want to sort from 21,457

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The first thing to do is to use regex101.com to build the proper regex expression to extract the data you want. You need to use everything in the cell including the i tag. For example:
    https://regex101.com/r/FqGOrP/1

    The matched group has your number. For example:
    https://jsfiddle.net/5bpxzja2/

    Kevin

Sign In or Register to comment.