16

Let's say I have an array: [0,3,4,2,5,1].

What I want to do is sort an array such as:

["one", "two", "three", "four", "five", "six"]

So that the order corresponds to the first array.

This would be the output:

["one", "four", "five", "three", "six", "two"]

Is there an easy way to accomplish this?

1
  • 4
    Your question is confusing, since it has got nothing to do with sorting. You keep the array in the same order, just map each element of the array to an element of another array. Commented Oct 28, 2010 at 21:20

7 Answers 7

19

You can do something like this:

function getSorted(arr, sortArr) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    console.log(sortArr[i], arr[i]);
    result[i] = arr[sortArr[i]];
  }
  return result;
}
var arr = ["one", "two", "three", "four", "five", "six"];
var sortArr = [0, 3, 4, 2, 5, 1];
alert(getSorted(arr, sortArr));

Note: this assumes the arrays you pass in are equivalent in size, you'd need to add some additional checks if this may not be the case.

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

Comments

1
orderedArray= function(arr,order){
    return  order.map(function(itm){return arr[itm]});
}

var sequence= [0, 3, 4, 2, 5, 1],arr=["one","two","three","four","five","six"]

arr=new orderedArray(arr,sequence);

/*  returned value: (Array)
one,four,five,three,six,two
*/

//You can make the order an unindexed property of the array, // and call array.ordered()

Array.prototype.ordered= function(order){
    var arr= this;
    order=order || this.order;
    return order.map(function(itm){
        return arr[itm];
    });
}


var arr= ["one","two","three","four","five","six"],
sequence= [0, 3, 4, 2, 5, 1];

arr.order=sequence;

arr.ordered()

/*  returned value: (Array)
one,four,five,three,six,two
*/

Comments

1

I was asked this on a phone interview. Then do it without creating another array, supposing the array is very large. I don't know if this is the answer since I wasn't able to do on the call (damn!), but here's what I came up with.

var my_obj_array = ['a', 'b', 'c', 'd'];
var my_indicies = [3, 1, 0, 2];
// desired result ['d', 'b', 'a', 'c']

var temp = {};
for (var i = 0; i < my_indicies.length; i++) {
    temp[i] = my_obj_array[i];  // preserve
    var j = my_indicies[i];
    if (j in temp) {
        my_obj_array[i] = temp[j];
        delete temp[j];
    } else {
        my_obj_array[i] = my_obj_array[j];
    }
}

http://jsfiddle.net/innerb/RENjW/

Comments

0

Not sur how you get your first array, but you could use an array of objects instead of [0,3,4,2,5,1]:

var arr = [
  {n:0, s:'one'},
  {n:3, s:'four'},
  {n:4, s:'five'},
  {n:2, s:'three'},
  {n:5, s:'six'},
  {n:1, s:'two'}
]

And avoid to process it.

Comments

0

You can use first array as directory for sorting second one using map method:

const firstArray = [0, 3, 4, 2, 5, 1];
const secondArray = ['one', 'two', 'three', 'four', 'five', 'six'];

const result = firstArray.map((item) => {
 return secondArray[item];
});
// result = ["one", "four", "five", "three", "six", "two"]

Comments

0
let inds = [0,3,4,2,5,1];
let x = ["one", "two", "three", "four", "five", "six"];
let x2 = [];  //Output
inds.forEach(ind => x2.push(x[ind]));
x2
//(6) ["one", "four", "five", "three", "six", "two"]

Comments

-1
class test1
{
  public static String[] sort(int[] array,String[] str)
  {
    String[] out=new String[str.length];
    for(int i=0;i<str.length;i++)
    {
      out[i]=str[array[i]];
    }
    return out;
  }
}

1 Comment

This is Java but the question asks for JavaScript. If you make this psuedo code it might help.

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.