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

  • Home
  • SEARCH
  • 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 8450681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:02:10+00:00 2026-06-10T11:02:10+00:00

What is the best method to sort a sparse array and keep the elements

  • 0

What is the best method to sort a sparse array and keep the elements on the same indexes?
For example:

a[0] = 3, 
a[1] = 2, 
a[2] = 6,
a[7] = 4,
a[8] = 5,

I would like after the sort to have

a[0] = 2, 
a[1] = 3, 
a[2] = 4, 
a[7] = 5, 
a[8] = 6.
  • 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-10T11:02:12+00:00Added an answer on June 10, 2026 at 11:02 am

    Here’s one approach. It copies the defined array elements to a new array and saves their indexes. It sorts the new array and then puts the sorted results back into the indexes that were previously used.

    var a = [];
    a[0] = 3;
    a[1] = 2; 
    a[2] = 6; 
    a[7] = 4; 
    a[8] = 5;
    
    
    // sortFn is optional array sort callback function, 
    // defaults to numeric sort if not passed
    function sortSparseArray(arr, sortFn) {
        var tempArr = [], indexes = [];
        for (var i = 0; i < arr.length; i++) {
            // find all array elements that are not undefined
            if (arr[i] !== undefined) {
                tempArr.push(arr[i]);    // save value
                indexes.push(i);         // save index
            }
        }
        // sort values (numeric sort by default)
        if (!sortFn) {
            sortFn = function(a,b) {
                return(a - b);
            }
        }
        tempArr.sort(sortFn);
        // put sorted values back into the indexes in the original array that were used
        for (var i = 0; i < indexes.length; i++) {
            arr[indexes[i]] = tempArr[i];
        }
        return(arr);
    }
    

    Working demo: http://jsfiddle.net/jfriend00/3ank4/

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

Sidebar

Related Questions

I have defined the following Array Dim myArray(10,5) as Long and would like to
What is the best sort of jsduck header to put over a method of
What is the best method of doing the following: I have Page A with
I have a mulitdimensional array like so: [ [name, age, date, gender] [name, age,
I have a couple of images that I would like to display the first
Ok, so I have an array like so: 1 2 3 4 5 6
I need to sort column C in the same order as column A. Best
I have an array of objects that I need to sort by a position
I am trying to figure out what the best method would be for me
Sort of a methods/best practices question here that I am sure has been addressed,

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.