0

I have an array of strings in a specific order and would like to arrange a different array of objects to match the original array order.

var arrangement = ["third", "first" "second"];

var myObjs = [
  {
    name: "thing",
    position: "first"
  },
  {
    name: "thing",
    position: "second"
  },
  {
    name: "thing",
    position: "third"
  }
];

So that the output would be:

var myObjs = [
  {
    name: "thing",
    position: "third"
  },
  {
    name: "thing",
    position: "first"
  },
  {
    name: "thing",
    position: "second"
  }
];

Any ideas on a good way to approach this?

Cheers,

3 Answers 3

4

Use indexOf function of the first array on the current elements you are sorting:

myObjs.sort(function(a, b) { 
    return arrangement.indexOf(a.position) - arrangement.indexOf(b.position);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and straightforward, but can be a lot of processing if arrangement isn't small.
2

I would build a map (an object) keyed by the position values from the first array, with the index of each entry as the value; e.g.:

var map = arrangement.reduce(function(obj, value, index) {
    obj[value] = index;
    return obj;
}, {});

Then I'd use Array#sort on myObjs with a comparator function that looks up the position values of the two entries it's given on the map:

myObjs.sort(function(a, b) {
    return map[a.position] - map[b.position];
});

Live example:

var arrangement = ["third", "first", "second"];

var myObjs = [
        {
            name: "thing",
            position: "first"
        },
        {
            name: "thing",
            position: "second"
        },
        {
            name: "thing",
            position: "third"
        }
    ];

var map = arrangement.reduce(function(obj, value, index) {
    obj[value] = index;
    return obj;
}, {});

myObjs.sort(function(a, b) {
    return map[a.position] - map[b.position];
});

snippet.log("Result: " + JSON.stringify(myObjs));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Comments

1

First you need to "index" the object array:

var objpos = {};
objects.forEach(function(obj, ix) { objpos['!' + obj.name] = ix; });

Then you can build the result

var result = arrangement.map(function(name) {
    return objects[objpos['!' + n]];
});

The ! is my habit of always adding it in front of keys when using objects as dictionaries (to avoid problems with the key "constructor").

Comments

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.