Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var deepSort = function(arr){
  2.   var i;
  3.   var hasPositionKey = [];
  4.   var noPositionKey = [];
  5.   var nestedObj;
  6.   var positions = [];
  7.   var loggingPositions = {};
  8.   var positionSortedObjects = [];
  9.  
  10.   // iterate through the array of objects
  11.   for(i = 0; i < arr.length; i++){
  12.     // set a variable for the individual object being considered
  13.      nestedObj = arr[i];
  14.     // if that object has a position of false
  15.     if(nestedObj.position === false){
  16.       // push false object into the array of false position objects
  17.       noPositionKey.push(nestedObj);
  18.     }
  19.     // if the object has a numbered position
  20.     if(typeof nestedObj.position === 'number'){
  21.       // push numbered position to the array of numbered position objects
  22.       hasPositionKey.push(nestedObj);
  23.     }
  24.   }
  25.  
  26.   // iterate through the array of objects with positions
  27.   for(i = 0; i < hasPositionKey.length; i++){
  28.     // set a variable for the individual object being considered
  29.     nestedObj = hasPositionKey[i];
  30.     // push only the position number into an array to track positions
  31.     positions.push(nestedObj.position);
  32.     // add a key value pair of position:id to an object
  33.     loggingPositions[nestedObj.position] = nestedObj.id;
  34.   }
  35.  
  36.   // sort the array that stores the positions of the non-false objects
  37.   positions.sort();
  38.   // iterate through that array and construct a final array of objects by sorted position
  39.   for(i = 0; i < positions.length; i++){
  40.     // set a variable for the positions, as encountered in order:
  41.     var num = positions[i];
  42.     // set a key for the object to be constructed
  43.     var key = loggingPositions[num];
  44.     // set a value for the object to be constructed
  45.     var value = num;
  46.     // construct a result object with object literal notation:
  47.     var result = {
  48.       id: key,
  49.       position: value
  50.     };
  51.     // push the constructed objects into the positionSortedObjects array
  52.     positionSortedObjects.push( result );
  53.   }
  54.  
  55.   // return a concatenated array of the positionSortedObjects + the noPositionKey objects
  56.   return (positionSortedObjects.concat(noPositionKey));  
  57. };
  58.  
  59. var sample = [
  60.     {
  61.         id : 1,
  62.         position: false
  63.     },
  64.     {
  65.         id: 2,
  66.         position: false
  67.     },
  68.     {
  69.         id: 3,
  70.         position: 4
  71.     },
  72.     {
  73.         id: 4,
  74.         position: false
  75.     },
  76.     {
  77.         id: 5,
  78.         position: 2
  79.     },
  80.     {
  81.         id: 6,
  82.         position: 5
  83.     },
  84.     {
  85.         id: 7,
  86.         position: 3
  87.     },
  88.     {
  89.         id: 8,
  90.         position: 1
  91.     }
  92. ];
  93.  
  94. deepSort(sample);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement