2

I made an array of objects and was able to sort by one of the properties of the objects (see bottom code). But I hardcoded which property to sort by (in this case "name"). How would I be able to pass to the sort method the property I am looking to sort by? I tried tmpArray.sort(function(a, b, sortBy) but got a no soup for you response. I know I can modify the array object with

array.prototype.thing = function(){ alert("hello world"); } 

But is is possible for me to modify an existing method, like array.sort? Thanks.

$(document).ready(function(){

    $(".sort").click(function() { 

    var tmpArray = new Array;

    for (i=0; i<5; i++) { 

        var tmpObject = new Object;
        switch(i) { 

            case 0 : 
                tmpObject.name = "Amy";
                tmpObject.rank = 1;
                tmpObject.priority = 3;
                tmpObject.item = "Blue Item";
                break;

            case 1 :
                tmpObject.name = "Jen";
                tmpObject.rank = 2;
                tmpObject.priority = 0;
                tmpObject.item = "Red Item";
                break;

            case 2 :
                tmpObject.name = "Mike";
                tmpObject.rank = 3;
                tmpObject.priority = 2;
                tmpObject.item = "Green Item";
                break;

            case 3 :
                tmpObject.name = "Raul";
                tmpObject.rank = 4;
                tmpObject.priority = 2;
                tmpObject.item = "Yellow Item";
                break;                      

                            case 4 :
                tmpObject.name = "Lola";
                tmpObject.rank = 5;
                tmpObject.priority = 1;
                tmpObject.item = "Blue Item";
                break;                      

        }

        tmpArray.push(tmpObject); 
    }

    tmpArray.sort(function(a, b) {
             var nameA=a.name.toLowerCase(), nameB=b.name.toLowerCase()

        if (nameA < nameB) //sort string ascending
            return -1
        if (nameA > nameB)
            return 1

        return 0 //default return value (no sorting)
        });

        console.log(tmpArray);

    });

});

3 Answers 3

3

Quick example, something like this should work. arr being the Array to sort, key being the Object key to use. Mind you, this is only one level deep sorting.

var arr = [
    {"one" : Math.random(), "two" : Math.random(), "id" : "First"},
    {"one" : Math.random(), "two" : Math.random(), "id" : "Second"},
    {"one" : Math.random(), "two" : Math.random(), "id" : "Third"}
];

var oSort = function (arr, key) {
    arr.sort(function(a, b) {
        a = a[key], b = b[key];
        return a > b ? 1 : a < b ? -1 : 0;
    });
};

oSort(arr, 'two');

http://jsfiddle.net/XwswP/

Edit to answer

But is is possible for me to modify an existing method, like array.sort? Thanks.

Yes it's possible, but don't do that. If you wanted to overwrite it, you could do
Array.prototype.sort = function() { ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Robert, that worked exactly as I needed. I didn't intend on messing with the array method, I figured I do that and then break the whole internets and then everyone is mad at me... : D
1

You can write a[someString] to get the property named in the someString variable.

Comments

-1

Your sort seems to be working fine here:

http://jsfiddle.net/maniator/2nFYN/

3 Comments

"How would I be able to pass to the sort method the property I am looking to sort by?"
@mellammokb -- im not sure why ur quoting that
Um.. because that's what the OP wants to do?? You just copied the OP's code into jsfiddle and posted an answer. The OP already knows the code works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.