0

i am having trouble sorting a multidimensional object by an embedded key value. i have this object:

var myClients =
          {  'cccc'  :   {       newValue        :       'test three'
                         ,       timeTest        :       3
                         }
          ,  'bbbb'  :   {       newValue        :       'test two;'
                         ,       timeTest        :       2
                         }
          ,   'aaaa'  :  {       newValue        :       'test one'
                         ,       timeTest        :       1
                         }
          };

console.log(util.inspect(myClients) )   ;

// current object displayed in raw order
for (var key in myClients)      {
    console.log(key + ' ' + util.inspect(myClients[key]));
}

i am trying to return the array sorted by the "timeTest" value. this was my best guess but it did not work.

tmpArray = _.sortBy(myClients, function(row) { return row.timeTest; } );

console.log(util.inspect(tmpArray));

for ( var result in tmpArray )  {
    console.log( (_.invert(myClients))[result] );
}

_.invert is not working for me, and it does not seem very efficient to use it anyways.

the other examples i see are on a single dimensional object.

any suggestions are greatly appreciated.

edit:

this could possibly qualify as an answer since it does just what i want, but it does not seem efficient at all having to loop through the primary object every time:

// display the original object
for (var key in myClients)      {
    console.log(key + ' ' + util.inspect(myClients[key]));
}

// create a new sorted array
var tmpArray = _.sortBy(myClients, function(key) { return key.timeTest; } );
var sortedClients = {};
for ( var result in tmpArray )  {
    for ( var key in myClients )    {
            if  ( myClients[key] === tmpArray[result] )     {
                    sortedClients[key] = myClients[key];
                    break;
            }
    }
}

// now display the sorted object
for (var key in sortedClients)      {
    console.log(key + ' ' + util.inspect(myClients[key]));
}

i would think that the underscore (dot)invert function would work but i was unable to apply it.

edwardsmarkf
  • 1,387
  • 2
  • 16
  • 31
  • 1
    you can not sort an object -- actually. you can sort the keys and take it as sorted reference for the object. – Nina Scholz Apr 28 '16 at 19:49
  • 1
    thank you for your suggestion to sorting a multidimensional object. the example given is only for single-dimensional objects. but with your suggestion i was able to come up with a solution, even if it my solution is not optimum. – edwardsmarkf Apr 28 '16 at 20:32
  • 1
    you can ask, if your solution does not works well. – Nina Scholz Apr 28 '16 at 20:35
  • 1
    my solution to a multidimensional object sort is now in place and working, but i had hoped for a more optimum solution. creating another temporary object and then looping back through the original object seems very inefficient, or at least have an underscore utility do most of the work for me. – edwardsmarkf Apr 28 '16 at 20:45
  • 1
    maybe [sorting with map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Sorting_with_map) is working for you. if not, just ask. – Nina Scholz Apr 28 '16 at 20:50
  • using "sort with map" as an alternative looks interesting. i will investigate. thank you. is this question still considered a duplicate question, since i am asking about multi-dimensional objects rather than single-dimensional objects? i would appreciate hearing what others say without having to ask the question again. – edwardsmarkf Apr 28 '16 at 20:59
  • maybe your presented array is/was not multidimensional. – Nina Scholz Apr 28 '16 at 21:06

0 Answers0