Datatables failing to sort columns with mix of text and numbers properly

Datatables failing to sort columns with mix of text and numbers properly

tjdracztjdracz Posts: 4Questions: 1Answers: 0

Hi

I'm using DataTables in my project and having an issue with sorting the table columns which contain mixtures of text and numbers. For example, lots of data I'm using is in the format of "<name> <year>" and the sorting just fails. If I remove the year from the name, it all works fine. My example below:

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
            </tr>
        </thead>
 
        <tbody>
            <tr>
                <td>Test 2015</td>
                <td>System Architect</td>
            </tr>
            <tr>
                <td>Test Independent 2015</td>
                <td>Accountant</td>
            </tr>
        </tbody>
    </table>

The position column sorts fine, but the name column doesn't do anything. The asc and desc classes are added but the data order isn't changed with the number in the field.

is there any solution here short of rewriting the data so it doesn't use numbers in the fields?

Thanks

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Can you give an example of the sort sequence you want to achieve, preferably with more than two rows?

  • tjdracztjdracz Posts: 4Questions: 1Answers: 0
    edited October 2015

    So in the example above:

    ascending:
    Test 2015
    Test Independent 2015
    descending
    Test Independent 2015
    Test 2015

    Another example with four columns:

    Test Leaders ITP 2015
    Test Leaders FT 2016
    Test Leaders RFT 2016
    Test Leaders RP 2016

    Should be:

    ascending:
    Test Leaders FT 2016
    Test Leaders ITP 2015
    Test Leaders RFT 2016
    Test Leaders RP 2016

    desc:
    Test Leaders RP 2016
    Test Leaders RFT 2016
    Test Leaders ITP 2015
    Test Leaders FT 2016

    Nothing fancy, just alphabetical sorting really..

    Currently the second example sorts the data by putting 2015 one at the top or bottom and kinda grouping all the other ones as they are. Looks like it's trying to sort by number, not alphabetical string

  • ThomDThomD Posts: 334Questions: 11Answers: 43

    Dude. Without some delimeters in your examples, it is really hard to figure out what the entries are. Can you update your post?

  • tjdracztjdracz Posts: 4Questions: 1Answers: 0

    Done, Markdown... ugh

  • tjdracztjdracz Posts: 4Questions: 1Answers: 0

    Solved by overriding default column type to "string". Still, seems to be a bug with DataType detection if "XYZ 123" is detected as a number (or so it seems), rather than a string

This discussion has been closed.