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 8233275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:04:32+00:00 2026-06-07T18:04:32+00:00

For example, if I have these arrays: var name = [Bob,Tom,Larry]; var age =

  • 0

For example, if I have these arrays:

var name = ["Bob","Tom","Larry"];
var age =  ["10", "20", "30"];

And I use name.sort() the order of the “name” array becomes:

var name = ["Bob","Larry","Tom"];

But, how can I sort the “name” array and have the “age” array keep the same order? Like this:

var name = ["Bob","Larry","Tom"];
var age =  ["10", "30", "20"];
  • 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-06-07T18:04:34+00:00Added an answer on June 7, 2026 at 6:04 pm

    You can sort the existing arrays, or reorganize the data.

    Method 1:
    To use the existing arrays, you can combine, sort, and separate them:
    (Assuming equal length arrays)

    var names = ["Bob","Tom","Larry"];
    var ages =  ["10", "20", "30"];
    
    //1) combine the arrays:
    var list = [];
    for (var j = 0; j < names.length; j++) 
        list.push({'name': names[j], 'age': ages[j]});
    
    //2) sort:
    list.sort(function(a, b) {
        return ((a.name < b.name) ? -1 : ((a.name == b.name) ? 0 : 1));
        //Sort could be modified to, for example, sort on the age 
        // if the name is the same. See Bonus section below
    });
    
    //3) separate them back out:
    for (var k = 0; k < list.length; k++) {
        names[k] = list[k].name;
        ages[k] = list[k].age;
    }
    

    This has the advantage of not relying on string parsing techniques, and could be used on any number of arrays that need to be sorted together.

    Method 2: Or you can reorganize the data a bit, and just sort a collection of objects:

    var list = [
        {name: "Bob", age: 10}, 
        {name: "Tom", age: 20},
        {name: "Larry", age: 30}
        ];
    
    list.sort(function(a, b) {
        return ((a.name < b.name) ? -1 : ((a.name == b.name) ? 0 : 1));
    });
    
    for (var i = 0; i<list.length; i++) {
        alert(list[i].name + ", " + list[i].age);
    }
    ​
    

    For the comparisons,-1 means lower index, 0 means equal, and 1 means higher index. And it is worth noting that sort() actually changes the underlying array.

    Also worth noting, method 2 is more efficient as you do not have to loop through the entire list twice in addition to the sort.

    http://jsfiddle.net/ghBn7/38/

    Bonus Here is a generic sort method that takes one or more property names.

    function sort_by_property(list, property_name_list) {
      list.sort((a, b) => {
        for (var p = 0; p < property_name_list.length; p++) {
          prop = property_name_list[p];
          if (a[prop] < b[prop]) {
            return -1;
          } else if (a[prop] !== a[prop]) {
            return 1;
          }
        }
        return 0;
      });
    }
    

    Usage:

    var list = [
            {name: "Bob", age: 10}, 
            {name: "Tom", age: 20},
            {name: "Larry", age: 30},
            {name: "Larry", age: 25}
        ];
    
    sort_by_property(list, ["name", "age"]);
    
    for (var i = 0; i<list.length; i++) {
        console.log(list[i].name + ", " + list[i].age);
    }
    

    Output:

    • Bob, 10
    • Larry, 25
    • Larry, 30
    • Tom, 20
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model object with one array property. Example: {name: 'Foo', tags
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs
I do not understand the following example, let's say I have these functions: #
For example I have in a conf.php file: $GLOBALS[no_access] = '^forbidden|restricted|xampp|apache\_pb'; blocking these files/folders
The issue Let's say that I have example.com, example.org and example.net. All of these
I have an Oracle DATE column with, for example, these values: RUN_DATE ------------------- 2012-06-09
I have updated my website. Previously there were links like these: http://example.com/bla-bla-bla?language=de . After
I have to create an array and place all controls there in order to
If I have in my db a table called User with name, Id, age
Let's say I have: var directions = [ name, start_address, end_address, order_date ]; I'm

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.