Reorder Array Of Objects

Discussion in 'JavaScript' started by lektrikpuke, Dec 8, 2017.

  1. #1
    
      var originalObject = [{
      "CatID": 15,
      "Currently": "is One",
      "ParentID": 16,
      "Should": "should be One"
      }, {
      "CatID": 17,
      "Currently": "is Two",
      "ParentID": 16,
      "Should": "should be Two"
      }, {
      "CatID": 18,
      "Currently": "is Three",
      "ParentID": 16,
      "Should": "should be Three"
      }, {
      "CatID": 22,
      "Currently": "is Four",
      "ParentID": 16,
      "Should": "should be Seven"
      }, {
      "CatID": 28,
      "Currently": "is Five",
      "ParentID": 16,
      "Should": "should be Eight"
      }, {
      "CatID": 92,
      "Currently": "is Six",
      "ParentID": 18,
      "Should": "should be Four"
      }, {
      "CatID": 30,
      "Currently": "is Seven",
      "ParentID": 92,
      "Should": "should be Five"
      }, {
      "CatID": 95,
      "Currently": "is Nine",
      "ParentID": 30,
      "Should": "should be Six"
      }];
    
      var localParentID = '';
      var localArr = [];
    
      for (var i in originalObject) {
      var iObj = originalObject[i];
    
      $('#displayDiv').append('Parent is ' + iObj['ParentID'] + ' ' + iObj['Currently'] + ' ' + iObj['CatID'] + ' ' + iObj['Should'] + '<br />');
      }
    
    Code (markup):
    How can I sort the array above so that the ouput matches "should" order as opposed to "currently" order? It should be ordered by currently with should ParentID being attached to currently CatID.
    Can't quite make this behave.
     
    lektrikpuke, Dec 8, 2017 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Well first, switch that "should be x" text to just that number. Then do a Array.sort with a callback, so the callback can get at the object's 'Should' property. (not that a property should start with an upper-case letter implying that it is in fact a CLASS)

    var data = [
    	{
    		"CatID": 15,
    		"Currently": "is One",
    		"ParentID": 16,
    		"Should": 1
    	}, {
    		"CatID": 17,
    		"Currently": "is Two",
    		"ParentID": 16,
    		"Should": 4
    	}, {
    		"CatID": 18,
    		"Currently": "is Three",
    		"ParentID": 16,
    		"Should": 3
    	}, {
    		"CatID": 22,
    		"Currently": "is Four",
    		"ParentID": 16,
    		"Should": 7
    	}, {
    		"CatID": 28,
    		"Currently": "is Five",
    		"ParentID": 16,
    		"Should": 8
    	}, {
    		"CatID": 92,
    		"Currently": "is Six",
    		"ParentID": 18,
    		"Should": 2
    	}, {
    		"CatID": 30,
    		"Currently": "is Seven",
    		"ParentID": 92,
    		"Should": 5
    	}, {
    		"CatID": 95,
    		"Currently": "is Nine",
    		"ParentID": 30,
    		"Should": 6
    	}
    ];
    
    function shouldCompare(a, b) { return a.Should - b.Should; }
    
    data.sort(shouldCompare);
    
    Code (markup):
    That's it. The array is in the 'should' order.
     
    deathshadow, Jan 6, 2018 IP