Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:41:51+00:00 2026-05-23T15:41:51+00:00

I made an array of objects and was able to sort by one of

  • 0

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

    });

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:41:51+00:00Added an answer on May 23, 2026 at 3:41 pm

    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() { ...

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()
I made a code that translate strings to match each word from the array
I have a line in a javascript function that sort an array of objects
I've made a function that processes an array of objects, process(Object[]). It works on
I have an array of objects which I need placed in a certain order,
When soritng an array made of a mix of strings, null values and zeros,
I have a 2d array grid made as int GRID[10][20]; What I want to
I made an anagram machine and I have an array of positive matches. The
I have made an app that gets an array of addresses from a web
I have an array that I want on multiple pages, so I made it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.