JSFiddle - React, Tailwind, and code Playground

by Palpatim

JavaScript

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]);
}