How to sort date in n-day/week/month/year format?

How to sort date in n-day/week/month/year format?

ernestin123ernestin123 Posts: 3Questions: 1Answers: 0
edited August 2016 in Free community support

I have a table that displays the 'freshness' of posts in this format:

30 second,
1 week
3 day,
5 month

But I can't figure out how to sort them, this is what I've achieved so far:

function getRank(name) {
    
        //newname = name.slice(2,name.length);
        //alert(name);
        newname = name;
        var rankNumber;
         
        if (newname == "1 day"){
            rankNumber = 1;
        } else if (newname == "week"){
            rankNumber = 2;
        } else if (newname == "month") {
            rankNumber = 3;
        } else if (newname == "year") {
            rankNumber = 4;
        } 
        console.log(newname);
        return rankNumber + parseInt(name.slice(0,1));
        
}
 
 
jQuery.fn.dataTableExt.oSort["rank-desc"] = function (x, y) {
     
        return getRank(x) < getRank(y);
};
     
jQuery.fn.dataTableExt.oSort["rank-asc"] = function (x, y) {
        return getRank(x) > getRank(y);
}

$(document).ready(function () {
    var oTable = $('#topics').dataTable({
        "autoWidth": false,
        "bLengthChange": false,
        "fixedColumns": true,
        "aoColumnDefs" : [{
            "bSortable" : true,
            "aTargets" : [5],
            "sType": "rank"
        }]
    });

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin

    You need to convert the string based information into numbers (i.e. something that can be easily sorted). You could split on the space and then multiple the number by the number of seconds in the unit given for example - then you would be sorting in seconds intervals.

    Allan

  • ernestin123ernestin123 Posts: 3Questions: 1Answers: 0

    This is a great answer, thank you allan, however I'm still having trouble.
    This is my code:

    function getRank(name) {
     ```    
            newname = name.slice(2,name.length);
            //alert(name);
            //newname = name;
            var rankNumber;
            if(newname == "second"){
                rankNumber = 1;
            } else if(newname == "minute"){
                rankNumber = 60;
            }else if(newname == "hour"){
                rankNumber = 3600;
            }else if (newname == "day"){
                //console.log(name);
                rankNumber = 86400;
            } else if (newname == "week"){
                rankNumber = 604800;
            } else if (newname == "month") {
                rankNumber = 2628000;
            } else if (newname == "year") {
                rankNumber = 31540000;
            } 
            //console.log(newname);
            //console.log( name + "-" +(rankNumber * parseInt(name.slice(0,1))));
            return (rankNumber * parseInt(name.slice(0,1)));        }
     
    jQuery.fn.dataTableExt.oSort["rank-desc"] = function (x, y) {
            return getRank(x) < getRank(y);
            
    };
    jQuery.fn.dataTableExt.oSort["rank-asc"] = function (x, y) {
            return getRank(x) > getRank(y);
    }
    

    However the sorting algorithm is not working properly:
    You can see the working algorithm in www.gradtheorem.com
    Thank you for all the help

  • allanallan Posts: 63,812Questions: 1Answers: 10,516 Site admin
    Answer ✓

    Could you change:

    return getRank(x) < getRank(y);

    to be:

    x = getRank(x);
    y = getRank(y);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    

    and likewise (inverted logic) for the asc method.

    Allan

  • ernestin123ernestin123 Posts: 3Questions: 1Answers: 0

    Oh wow :open_mouth: thank you Allan!
    It works now :)

    Thank you very very much :)

This discussion has been closed.