0

I have an array of objects:

var data= [{
 "title": "All pages",
 "page": "all",
}, {
 "title": "Post with builder",
 "page": "pt_post_6188",
}, {
 "title": "Blog Categories",
 "page": "tx_category",
}, {
 "title": "Single Blog Posts",
 "page": "pt_post",
}];

and a sorting order array constructed out of the object item titles.

var order = ["Post with builder", "All pages", "Blog Categories", "Single Blog Posts"];

how do I sort the first array by the order array so that the new data turns out like this

var newdata= [{
 "title": "Post with builder",
 "page": "pt_post_6188",
}, {
 "title": "All pages",
 "page": "all",
}, {
 "title": "Blog Categories",
 "page": "tx_category",
}, {
 "title": "Single Blog Posts",
 "page": "pt_post",
}];

?

Not same as ref post. I am sorting by a specific order array without any specific logic.

asked Apr 9, 2016 at 12:19
9
  • 3
    data.sort((a, b) => order.indexOf(a.title) > order.indexOf(b.title)); Commented Apr 9, 2016 at 12:21
  • By what criteria is that sorting by? Alphabetically? No...Reversed alphabetically?...No...Am I missing something? Commented Apr 9, 2016 at 12:33
  • @zer00ne, sorting specific title order, collected in to an array Commented Apr 9, 2016 at 12:35
  • @Benn, ok so there's no logical order, it's just presenting the same data in an object as it is already ordered in the array. Commented Apr 9, 2016 at 12:39
  • @zer00ne, correct , no logic here , I am using UI sort and outputting an array for layouts, but their order needs to be specific since I am looping trough them and checking some params. Commented Apr 9, 2016 at 12:43

3 Answers 3

1

You could use an object as hash table

{
 "Post with builder": 0,
 "All pages": 1,
 "Blog Categories": 2,
 "Single Blog Posts": 3
}

for the indices and sort with them.

var data = [{ "title": "All pages", "page": "all", }, { "title": "Post with builder", "page": "pt_post_6188", }, { "title": "Blog Categories", "page": "tx_category", }, { "title": "Single Blog Posts", "page": "pt_post", }],
 order = ["Post with builder", "All pages", "Blog Categories", "Single Blog Posts"],
 orderObject = {};
order.forEach(function (a, i) {
 orderObject[a] = i;
})
data.sort(function (a, b) {
 return orderObject[a.title] - orderObject[b.title];
});
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

answered Apr 9, 2016 at 12:36

Comments

0

Since you already have the order arrays indexes for your ordering reference as a kind of hash, we may very simply use them like;

sorted = Array(order.length);
data.forEach((c,i) => sorted[order.indexOf(c.title)] = c);
answered Apr 10, 2016 at 16:22

Comments

0

// Using loadash

var data= [{
"title": "All pages",
"page": "all",
}, {
"title": "Post with builder",
"page": "pt_post_6188",
}, {
"title": "Blog Categories",
"page": "tx_category",
}, {
"title": "Single Blog Posts",
"page": "pt_post",
}];

var order = ["Post with builder", "All pages", "Blog Categories", "Single Blog Posts"];

function (data, order){
 return _.reduce(order, function (output, v) {
 output.push(_.filter(data,{title:v})[0]);
 return output;
 },[]);
}
answered Apr 11, 2016 at 18:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.