Sorting similar to a database set

Sorting similar to a database set

cjsmithcjsmith Posts: 6Questions: 0Answers: 0
edited February 2012 in General
I'm not quite sure what this would be called, but I'm trying to implement sorting that turns a single click ascending sort to something similar to a sort a database would perform where all fields are sorted in terms of a priority.

So if I clicked to do an ascending sort on the third column, My table would then sort to look like this:
(And these columns would be displayed in order of their sql query so similar to sorting on a database visualizer tool

A G A 20
A B C 14
A K E 19
A K E 22
B K E 27
M L E 4
B E F 1

But I would like this functionality across the board for all single click sorts to behave this way, and then the multi sorts to behave similar where the other non-selected columns automatically follow this sort pattern.

Replies

  • cjsmithcjsmith Posts: 6Questions: 0Answers: 0
    edited February 2012
    To help clarify, right now the sorting is dependent on what column was sorted previously. If i go out and sort column 4 desc, and go back and sort column 3 Ascen, it gives me a different order than if I go out and sort column 4 Ascen, and go back and sort column 3 Ascen. Does this make sense? I'd like it to produce the same table not concerned about what was sorted previously on a single sort.

    So right now when i do $.each(oTable.fnSettings().aaSorting, function(index, sortObj) {
    var position = parseInt(sortObj[0]) + 1;
    var direction = sortObj[1];
    sorting += "&sort=" + position + "_" + direction;
    });

    I'm only getting an array back containing the last click - the column 3 Ascending, although the 4th column is sub-sorted ascending/descending depending on how it was set previously. Now when I go server side to pull the data back and just use the ordering for the last column (all that is returned in the aaSorting settings, this returns a different ordered set than what the client is seeing.
  • cjsmithcjsmith Posts: 6Questions: 0Answers: 0
    edited February 2012
    Issue - table sorts and arranges itself in relation to the last sort that was done - so when aaSorting shows a sort of just 'COLUMN 2' which may have been the last sort, it could have also been sorted descending on column 4, 5, 6 prior to that, which changes how the table looks when you sort column 2. I needed a way to convey the table appearance server side sending as little information as possible.

    I solved this by tracking the historical changes of each column sorted, and keeping it in an array thats the length of the sortable column count, moving the last column sorted on to the front of the historical array, and in the case of a multi sort i'm doing a reverse loop (on aaSorting ) unshift to the front of my historical array. Then I can use the above code but instead of aaSorting, i'm using my historical array of aaSortHistory.

    I thought I noticed a bug on the multi column sorting if you were to do a multiple sort, and cycle your last sorting column all the way through the 3 options so its not ascending or descending, it remained in aaSorting as descending, but I could be wrong and this may have happened while I was tweaking around.

    [code]
    var fnInnerSorting = function () {
    var iColumn, iNextSort;

    /* If the shift key is pressed then we are multipe column sorting */
    if ( e.shiftKey )
    {
    /* Are we already doing some kind of sort on this column? */
    var bFound = false;
    for ( var i=0 ; i
  • cjsmithcjsmith Posts: 6Questions: 0Answers: 0
    And the sorting fn
    [code]
    jQuery.fn.dataTableExt.oSort['string-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;

    return retVal;
    }
    jQuery.fn.dataTableExt.oSort['string-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;

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