Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.hugoSort = function(arrayWithObjectIds,idPropName){
  2.   var ao = this, a = arrayWithObjectIds, id = idPropName || "id";
  3.   ao.sort(function(x,y){
  4.     return a.indexOf(x[id])>=a.indexOf(y[id]) ? 1 : -1;
  5.   });
  6. };
  7.  
  8. var cats = [
  9.   {id:1, name: "Boris"},
  10.   {id:2, name: "Moxy"},
  11.   {id:3, name: "Billie"}
  12. ];
  13.  
  14. cats.hugoSort([3,1,2]);
  15.  
  16. console.log(cats); // Billie, Boris, Moxy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement