sort array

Discussion in 'Javascript' started by jaykappy, Oct 22, 2015.

  1. jaykappy

    jaykappy

    Joined:
    Oct 18, 2015
    Messages:
    5
    Likes Received:
    0
    I am looking to sort an array in two ways....

    First: Sort by an integer field and then by text field - example sort but total count value and then by a animal name

    Second: Sort by a text field and then another text field - example sort by an animal species, and then by its common name

    Any thoughts?

    Below I can sort by either Integer or Text but not both.




    var sort_by = function(field, reverse, primer){

    var key = primer ?
    function(x) {return primer(x[field])} :
    function(x) {return x[field]};

    reverse = !reverse ? 1 : -1;

    return function (a, b) {
    return a = key(a), b = key(b), reverse * ((a > b) - (b > a));
    }
    }

    //SORT TEXT FIELD
    //newarr.sort(sort_by('GENUSalse, function(a){return a.toUpperCase()}));

    // SORT INTERGER FIELD
    newarr.sort(sort_by('ObsIDalse, function(a){return a}));
     
    jaykappy, Oct 22, 2015
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.