4

Here is an example:

            //    0     1       2      3       4
 var people = ['jack','jill','nancy','tom','cartman'];
 var order  = [3,1,4,0,2];

 // somehow sort people array to the order specified in the order array

          //  3      1       4        0      2
 people == ['tom','jill','cartman','jack','nancy'];

I have used .sort with a function before, but am still at a loss on this one.

UPDATE

after seeing some answers, I can't believe this was not obvious to me. So as there are many ways to do this, winner will be determined by jsperf.

(also I am upvoting everyone with a working answer)

The RACE! http://jsperf.com/array-sorted-to-order-array3

12
  • 2
    Winner should be determined by readability and clearest of intent first :) Commented Oct 30, 2012 at 23:20
  • 1
    @Fresheyeball: You mean it's equally functional in your situation. It isn't in all situations. Commented Oct 30, 2012 at 23:43
  • 1
    @Fresheyeball Depends on what else is pointing to that original array. Commented Oct 30, 2012 at 23:47
  • 1
    @Fresheyeball: If you replace the Array referenced by the people variable, you're only updating that one reference. If there are other references to the original Array, they won't automatically be updated to reference the new Array, so your references will be out of sync. All depends on what's needed. Commented Oct 30, 2012 at 23:47
  • 1
    I did not think of the pointer situation. Good point. Commented Oct 30, 2012 at 23:52

4 Answers 4

6
sorted = []
order.forEach(function(i) { sorted.push(people[i]) });

or, more fancy but less readable (IMO):

sorted = order.reduce(function(r, i) { 
    return r.concat([people[i]])
}, []);
0
4

Just another way :)

people = order.map(function(value) { return people[value]; });

jsFiddle.

1
  • .map() is the only way to do this IMO. Commented Oct 30, 2012 at 23:30
2

order is an array of indicies. So just iterate through that, pulling out the values you want in the order spcified, making a new array.

var people = ['jack','jill','nancy','tom','cartman'];
var order  = [3,1,4,0,2];

var sorted = [];
for (var i = 0; i < order.length; i++) {
  var desiredIndex = order[i];
  sorted.push(people[desiredIndex]);
}

console.log(sorted);
// ["tom", "jill", "cartman", "jack", "nancy"]

Sometimes sorting isn't "sorting". Sometimes you just need to make a new thing by pulling data from your other things.

4
  • @Fresheyeball I don't think it's odd. Dropping back to basic constructs such as a for loop is generally fastest. Commented Oct 30, 2012 at 23:41
  • @alex it shouldn't be though. With all the hype surrounding map and reduce, I thought the browser had a way of optimizing this kind of operation far beyond the basic construct. Commented Oct 30, 2012 at 23:43
  • @Fresheyeball: It's mostly just that those functional approaches provide nice clean code. The functions can also be stored and reused for different situations. But functions require some overhead to invoke, so a traditional for loop is the way to go when speed is a high priority. Commented Oct 30, 2012 at 23:52
  • Functionless iteration is usually fastest. Even if it is more verbose. Commented Oct 30, 2012 at 23:54
1

I dunno, something like this perhaps?

var people = ['jack','jill','nancy','tom','cartman'];
var order  = [3,1,4,0,2];


var result = [], i, n=order.length;

for (i=0; i<n; i++)
{
  result[ order[i] ] = people[i];
}

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.