简体   繁体   中英

Custom sorting jqgrid

I am trying to write a function to sort my column because it has specific values. I have a lot alarms, and they have 2 states: Active and Not Active. And in case of "Active" I write to column string - "Active" and in case of "Not active" I write to column last seen date like: 24 Jun 2014, 07:36:14.

And the problem that when I use default jqgrid sort function, it sorts unproperly, because I have 2 types data.

And I found that in jqgrid I can write custom sorting functions. So here is my efforts:

var timeRegexPatter = /(\d{2})-(\d{3})-(\d{4}) (\d{2}):(\d{2}):(\d{2})/;
var alarmLastSeenSort = function(a, b, direction) {
    if (a === 'Active') { //how to use directions properly?
        return 1;
    }
    else if (b === 'Active') {
        return -1;
    }
    var timeArrA = a.match(timeRegexPatter); //array A
    var timeArrB = b.match(timeRegexPatter); //Array B

    //here should be probably transform time regex into timestamp and compare it,
    //is there any predefined functions to do this
}

Caveat: I don't know the jqgrid library specifically.

In general, though, a sort function should return 1, 0, or -1 based on the comparison of the two incoming arguments. Assuming an ascending sort:

  • If a < b, return -1
  • If a == b, return 0
  • If a > b, return 1

where the < , == , and > operators refer to your desired collating order of the objects, which may not necessarily be the same as strict mathematical or string comparisons. For example, you may have an object that you want to sort by name, then ID, which would involve comparisons of two different properties with different types.

In your case, you have two axes on which to sort, "activeness" and "timestamp". So your first question is: how should an active compare to an inactive? It doesn't make sense to compare one item at a time, unless it's to forbid sorting of objects that are not the same type and throw an error.

Once you dispose of the "activeness" sort, you can move on to comparing timestamps in the case of an inactive item.

Again, I don't know jqgrid specifically, but I assume direction refers to "ascending" or "descending" order. That will determine whether you return a 1 (ascending) or -1 (descending) for the a > b case.

Demo here.

var i, sortedArray;

var testArray = [
    'active',
    '2014-06-25 01:23:45',
    'active',
    'active',
    '2013-01-31 12:34:56',
    '2014-06-25 02:34:45'];

var comparitor = function(a, b, direction) {
    console.log('comparitor(a, b, direction)::', a, b, direction);
    // Replace this with whatever test allows you to determine whether you're sorting in ascending or descending order
    var after = direction === 'descending' ? -1 : 1;
    // If both are active, neither should be reordered with respect to the other
    if (a === 'active' && b === 'active') {
        console.log('Both a & b are active; returning 0');
        return 0;
    }

    // We know at least one is "inactive". Assume "active" should come before "inactive".
    if (a === 'active') {
        console.log('a is active; returning -1');
        return -1 * after;
    } else if (b === 'active') {
        console.log('b is active; returning 1');
        return after;
    }

    // We know that neither one is active, and can assume both are date strings. You could convert to dates here, but why, since your dates are already in a format that sorts quite nicely?
    if (a === b) {
        console.log('a === b; returning 0');
        return 0;
    }

    console.log('a !== b; returning either 1 or -1');
    return a > b ? after : -1 * after;
}

sortedArray = testArray.sort(comparitor);

for (i = 0; i < sortedArray.length; i++) {
    console.log(i + ' = ' + sortedArray[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM