1

I hope, that I used terms right, I will explain anyway.

So the idea is: Here is a 'dictionary' (it's an array of id's of DOM elements, but i suppose this doesn't matter)

['foo', 'evil', 'banana']

And here's an array of objects:

[
 {id: 'evil', data: 'somedata'},
 {id: 'foo', data: 'dataagain?'},
 {id: 'banana', data: 'somemoredata'} 
]

I think You already get it.
I want to sort this array of objects in the way its id's will be in the same order as corresponding elements in 'dictionary'.
As elegant and as fast as possible, obviously. jQuery or plain JavaScript. For the moment i can only think about splice trickery, like looking up for place of key and then splicing object out and back in to required place.

To make myself completely clear, I want this array of objects at the end to look like this:

[
 {id: 'foo', data: 'dataagain?'},
 {id: 'evil', data: 'somedata'},
 {id: 'banana', data: 'somemoredata'} 
]
Max Yari
  • 3,617
  • 5
  • 32
  • 56
  • It's not a duplicate. The question is how to sort by a custom order given by the initial array, not how to sort by object keys in array in general. Please read questions more carefully. – Francisc Apr 08 '15 at 16:14
  • I'm not really sure that this is a dup. As i understand Sorting objects in an array by a field value in JavaScript is about, well, the name speaks for itself, algoritm of sort is predefined, it will sort object by its values in ascending descending, alphabetical e.t.c order, but i want to match an order of array of objects to order of keys in dictionary. – Max Yari Apr 08 '15 at 16:16
  • It's not a dupe, or not for the question mentioned as its dupe. – Francisc Apr 08 '15 at 16:17
  • 1
    Here's what the function you need should look like: ```var sort = function(data, dictionary) { 'use strict'; data.sort(function(a, b) { return dictionary.indexOf(a.id) - dictionary.indexOf(b.id); }); };``` – Francisc Apr 08 '15 at 16:19
  • That's the max formatting comments allow, so sorry about that. You have to add extra checks to see if data and dictionary are arrays and so on, but you get the point. The main idea is to compare the index position in the dictionary for the ID of an item in the dataset. – Francisc Apr 08 '15 at 16:19
  • @Francisc correct me if im wrong, as i understand `a` and `b` is passed by `sort` and they are objects from following indexes of array like 1-2, 2-3 e.t.c, this function must return for `sort` `-value` `+value` or `0`. As i understand `sort` will iterate over all the indexes, but HOW he will change the position of the element. Moving by one step in direction according to returned by func in sort value? Ain't it going to be more effective to manually loop through and splice out and in as I already KNOW in which place those objects must be. – Max Yari Apr 08 '15 at 16:31
  • @Francisc i.e if my current index `1` must be in place of current index `1 000 000`, `sort` will iterate a million times moving it step by step down, until the value returned to sort by sorting function will became non `-`. Isn't it better then just to splice-out element `1mil` and splice-in in index `1`. Or then it's a question of how splice works under the hood, maybe it will `iterate` 1m times just to move all those other indexes inbetween. Maybe i'm just looking a bit deeper that i need to, my array anyway will be around length of 100 on extrmely rare occasions – Max Yari Apr 08 '15 at 16:41
  • 1
    If you know which indexes are where and don't need to iterate all, manually will obviously be faster. You can read about the `sort` function on MDN, here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort. It explains how `a` and `b` are passed in and how it handles negative, positive or 0 returns. – Francisc Apr 08 '15 at 18:47
  • 1
    @Francisc nah, it seems that i would need to iterate anyway, just to find where is this object in array to splice it and put on index with corresponding to dictionary number.i.e i don't know initial indexes in data array, therefore need to iterate anyway. I just made a few simple additions and now using function of type, suggested by You, works like a charm, thx) – Max Yari Apr 08 '15 at 19:06
  • Happy to help, Max. Keep in mind you should test the function thoroughly and augment it. For example, what should happen if an element in the dataset isn't found in the dictionary? – Francisc Apr 08 '15 at 19:09

0 Answers0