• Resolved Chris

    (@totallydubbed)


    Hi,
    For some reason when I’m sorting a table, TablePress can’t identify that 100 is greater than 90, and puts it as if it’s at 1, rather than 100.

    EDIT: I think it’s because of how many numbers there are? ie. 90 has two, 100 has three. When putting 090 and 100, it seems to work; but that seems incredibly flawed?

    I’ve linked the table which isn’t fully complete yet, but if you sort the “Battery Pack” or “Tested Range Miles” in the “EV Mileage Leaderboard” it’ll display incorrectly. Likewise, in the “Hybrid Mileage leaderboard” sorting by “Tested MPG UK”, the sorting is wrong.

    What’s going on or what setting I’m I missing here? Thanking you in advance!

    • This topic was modified 4 months, 3 weeks ago by Chris.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi!

    Thanks for your post and sorry for the trouble!

    The reason for this basically is that your columns don’t have plain numbers: Many cells also have the text “(est)” in them. This then turns the sorting algorithm for number sorting to text sorting — which in the end has the effects that you are seeing with the 2- and 3-digit numbers.

    To adjust this for the columns with “(est)”, you can instruct the sorting to ignore these, by adding this to the “Custom Commands” text field on the table’s “Edit” screen:

    columnDefs: [
    { type: 'num-fmt', targets: [3, 4] },
    ],

    For the two columns with number ranges, this approach will not work, as the sorting algorithm then doesn’t know which number to use for sorting. My recommendation here would be to use an approach similar to https://wordpress.org/support/topic/sorting-column-with-currency-and-text-values/#post-15088474
    That means: Add an extra (hidden) column with just a single value (likely the first one). Then, assign that hidden column to be used as the sorting data for the visible column.

    Best wishes,
    Tobias

    Thread Starter Chris

    (@totallydubbed)

    Hi Tobias,

    Thanks for the swift reply. I added the code suggested but it did nothing. I’m also confused as to what that means, can you explain?
    As for the ranges; I don’t know what to do, as what you’ve linked then links to another 4-5 threads, with multiple different solutions.

    Adding an extra column to hide is fine, but that would solve the “Tested Range” but won’t work on the “Claimed Range”, so how will the table know to sort it properly when there are two sets of ranges from two sets of data points? If that makes sense? I presume I can’t create two hidden columns to then have “Tested Range” sorting via the hidden column No1, while “Claimed Range” would base it on hidden column No2?

    I’m really not sure what code I’d have to input here? Was hoping TablePress would be simple to use, but it’s now causing me a headache :/

    Edit, my wife suggested I ask ChatGPT and it’s worked…!?

    Here’s the code:

    "columnDefs": [ { "orderData": [ 1 ], "targets": [ 2 ] }, { "visible": false, "targets": [ 1 ] }, { "orderData": [ 3 ], "targets": [ 4 ] }, { "visible": false, "targets": [ 3 ] }, {
    targets: [5, 6],
    render: function (data, type, row) {
    if (type === 'sort' || type === 'type') {
    // Extract first number (supports decimals & negatives)
    let num = data.toString().match(/-?\d+(.\d+)?/);
    return num ? parseFloat(num[0]) : 0;
    }
    return data; // keep original text for display
    }
    } ]

    Note ChatGPT said this:
    num-fmt only works for:

    • 1,234
    • 1.234,56
    • clean numeric formats

    ❌ It does not strip text
    "15 days" will never sort numerically”

    Could you let me know if the above is all correct?

    Also it’s worth noting that with what you suggested for the number range, the arrows no longer appear when sorted. Why is this?

    • This reply was modified 4 months, 2 weeks ago by Chris.
    • This reply was modified 4 months, 2 weeks ago by Chris.
    • This reply was modified 4 months, 2 weeks ago by Chris.
    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    ah, my bad, you are right, the num-fmt format will not directly work for text. For that, you can use the approach with the small extra plugin from https://tablepress.org/extensions/datatables-sorting-plugins/

    As for the two range columns: Correct, you would need a separate (hidden) column for each of these.

    For the missing sorting arrows: I think I know why that is (a quirk in an external code library), but it should be an easy fix: In those code chunks with orderData, e.g. { "orderData": [ 1 ], "targets": [ 2 ] }, please also include the number after "targets" in the "orderData", i.e. write this snippet as { "orderData": [ 1, 2 ], "targets": [ 2 ] }.

    And I totally understand that this is “giving you headaches”, but please keep in mind that you are asking quite a lot from a simple sorting algorithm here: It’s not a human brain that can automatically recognize or distinguish numbers or patterns. Therefore, you’ll have to explicitly tell it which numbers mean what or which numbers to use for sorting.
    By the way, as part of the premium email support that comes with the TablePress premium license plans I can also help directly with adjusted code snippets.

    Best wishes,
    Tobias

    Thread Starter Chris

    (@totallydubbed)

    Thanks again for the swift reply; sorry just frustrating as there’s more work than I thought for it to work. My expectations were very high, but nonetheless this free plugin is a great means of displaying content – and your work and support is very much apprecaited.

    As for what you’ve suggested; the arrows now work, thanks!
    But for the sorting of numbers, it still doesn’t work with the plugin you’ve mentioned.

    Here’s the code:

    "columnDefs": [ { "orderData": [ 1, 2 ], "targets": [ 2 ] }, { "visible": false, "targets": [ 1 ] }, { "orderData": [ 3, 4 ], "targets": [ 4 ] }, { "visible": false, "targets": [ 3 ] }, { type: 'num-fmt', targets: [5, 6] } ]

    Not sure what I’m missing here?

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi Chris,

    With the plugin installed, please switch from num-fmt to formatted-num (which is a similar but more extensive algorithm).

    Best wishes,
    Tobias

    Thread Starter Chris

    (@totallydubbed)

    Amazing thank you for all your help Tobias, that’s now worked and looks ‘cleaner’ with the code 🙂

    Plugin Author Tobias Bäthge

    (@tobiasbg)

    Hi,

    no problem, you are very welcome! 🙂 Good to hear that this helped!

    Best wishes,
    Tobias

Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.