Custom sort with JSON data source not working as expected

Custom sort with JSON data source not working as expected

raka86raka86 Posts: 10Questions: 4Answers: 0

Hi,
I am trying to use JSON data source with a custom sort function.
Accessing the data works. However, sorting the column does nothing.
Example (full demo at http://live.datatables.net/sirafugi/2/edit?html,js,output ):
JSON data:
[ "Tiger Nixon", ["2011/04/25", "2"] ]
DataTable config:

columns: [
    { title: "Name" },
    {
        title: "Start date",
        data: {
            _: function( row, type, set, meta ) {
                return row[meta.col][0];
            },
            sort: function( row, type, set, meta ) {
                return row[meta.col][1];
            }
        }
    }
]

What am I doing wrong?

Thanks in advance.

PS: My previous post somehow removed from the forum...

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @raka86 ,

    The best thing here is to use orthogonal data data in columns.render - I've modified your example here.

    Cheers,

    Colin

  • raka86raka86 Posts: 10Questions: 4Answers: 0

    Hi @colin,

    Thanks for the prompt answer.

    However, your solution behaves the same like mine: Sorting on the "Start date" column has no effect...

    Am I missing something?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Hi @raka86 ,

    It's working for me here. It's using the single digit for the sorting, not the date itself. So, ordering ascending puts Garret top (as he's 1), ordering descending put Brielle on top (as she's 6).

    If that's not what you want/expected, could you explain more, please.

    Cheers,

    Colin

  • raka86raka86 Posts: 10Questions: 4Answers: 0
    edited March 2019

    Alright, so I tested it with another browser than Firefox.
    It this seems not be a problem of the implementation. Your solution works as well as mine. But ONLY in Chrome and Opera.

    In IE11, Edge, and Firefox, nothing happens when I click on the column to sort the data (except that the arrows toggle, data remains unchanged). This applies to both solutions, yours and mine.

    Can you confirm?

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Ah, yep, I'm seeing that too - and sorry, realising now that yours is doing the same as mine! We'll take a nose and report back.

  • allanallan Posts: 61,665Questions: 1Answers: 10,096 Site admin
    Answer ✓

    That was a head scratcher for a few minutes! What is happening is that since the orthogonal data doesn't define the type information the ISO date is being used for the type detection. But that then doesn't align with what is used for the sorting data (an integer).

    The fix is to set the type information as well: http://live.datatables.net/sirafugi/6/edit .

    Worth noting that you can also use string identifiers for your data points rather than needing to use functions: http://live.datatables.net/sirafugi/7/edit .

    The reason why Chrome based browsers were working was they are really aggressive with parsing dates. Other browsers, not so much.

    Allan

  • raka86raka86 Posts: 10Questions: 4Answers: 0

    Thanks a lot for the explanation @allan!

This discussion has been closed.