Sort integer ascending and put empty cells to the bottom

Sort integer ascending and put empty cells to the bottom

ShinobiShinobi Posts: 4Questions: 0Answers: 0
edited January 2013 in General
Hello everyone!

I have some problems with ascending sorting of a column. Here is how my table looks like.

[code]
First events Second Event Third event
Team Position Points Position Points Position Points
Team A 1 100 2 90 1 100
Team B 1 100 3 80
Team C 2 90 2 90
[/code]

so it's an overview of teams who competed in three events. the table is sortable for position of each event. Some teams did not compete in all events. When i now want to sort the second event, Team C is in top, which is obivously wrong.

Now I'm trying to find a solution, how i can put empty cells on the bottom of the sorted column. I found a few older threads (yes i searched before starting a new topic) about the same issue, but those solutions don't really seem to work.

I use Wordpress and the Tablepress plugin, which is based on Datatables.

This was the best thread i found about my problem: http://datatables.net/forums/discussion/4025/sorting-to-ignore-empty-cells/p1

Thank you in advance!

Replies

  • ShinobiShinobi Posts: 4Questions: 0Answers: 0
    oh i just saw, that my beautiful table got stripped of all his spaces. on the example table just imagine, that Team C did not compete at all in the second event.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    I've updated your post to add code tags to preserve whitespace and altered the white space a bit.

    Sounds like you want multi-coloum sorting by default, which can be done with aDataSort - provide an array of the columns you want sorting to occur on. The default is just one column, but you can add more.

    Allan
  • ShinobiShinobi Posts: 4Questions: 0Answers: 0
    thank you allan. the table and all functions i need are already implemented. my only problem are those empty cells on top, when sorting ascending.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Ah I see - so regardless of sorting direction, you always want the empty cells on the bottom? You'd need to write a sorting plug-in which forces empty data to always be at the bottom: http://datatables.net/development/sorting .

    There are lots of sorting plug-ins in the plug-ins section of the site as well.

    Allan
  • ShinobiShinobi Posts: 4Questions: 0Answers: 0
    yes exactly. although i only do ascending sorting. the problem is, that i don't really know javascript. fbas' code seemed to work for the other user, but he had cells with strings. i'm not sure if that makes a difference.

    is there someone who could test this code and tell me what i need to change to make it work with my table?



    [code]
    jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
    var retVal;
    x = $.trim(x);
    y = $.trim(y);

    if (x==y) retVal= 0;
    else if (x == "" || x == " ") retVal= 1;
    else if (y == "" || y == " ") retVal= -1;
    else if (x > y) retVal= 1;
    else retVal = -1; // <- this was missing in version 1

    return retVal;
    }
    jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
    var retVal;
    x = $.trim(x);
    y = $.trim(y);

    if (x==y) retVal= 0;
    else if (x == "" || x == " ") retVal= -1;
    else if (y == "" || y == " ") retVal= 1;
    else if (x > y) retVal= 1;
    else retVal = -1; // <- this was missing in version 1

    return retVal;
    }
    [/code]
This discussion has been closed.