-2
\$\begingroup\$

The working code:

var dict = [
    {name: 'random234'}, 
    {name: 'random843'}
]       

function organizeObject(dict) {
    var resultArray = [];

    var x = _.find(dict, function(o) {
        return o.name === "random234";
    });
    if (x) resultArray.push(x);

    var y = _.find(dict, function(o) {
        return o.name === "random843";
    });
    if (y) resultArray.push(y);

    return resultArray;
}

I'm currently taking 'dict' in, finding name values using lodash's _.find method and if what I'm looking for exists - I'm pushing that to a new array to then return.

Is there a way to clean this up? One idea is to pass the function an array of name values I want the returned data set to by sorted by.

For instance:

 var sortByTheseValues = ['random843', 'random243']; 

Ideally the original data set would be sorted by this array's values. jQuery is an option.

To clarify: I don't actually want to order by desc or asc, I want to order by a random array of values. If I specify ['random3', 'random2'] in an array, I want the object in the dictionary which corresponds to name: 'random3' to be first in the result.

\$\endgroup\$
  • \$\begingroup\$ I'm not sure about the lodash framework, etc., but it seems like your function just changes the order of things in the dictionary, based on the name: field. You also filter on a set of valid values. Is that all you're trying to do? Filter values and sort? \$\endgroup\$ – Austin Hastings Feb 14 '18 at 0:44
  • \$\begingroup\$ Lodash provides orderBy: stackoverflow.com/questions/22928841/… \$\endgroup\$ – Austin Hastings Feb 14 '18 at 0:45
  • \$\begingroup\$ It's very hard to tell what is real and what is fabricated in this code excerpt. What do you mean by random234 and random843? Are there only two special values? Does the dict only have those two items? If not, does the sort have to be a stable sort? Do you actually want the values in alphabetical order, or is the order purely arbitrary? \$\endgroup\$ – 200_success Feb 14 '18 at 1:07
  • \$\begingroup\$ It would be much better if you posted your real code. Please see How to Ask. \$\endgroup\$ – 200_success Feb 14 '18 at 1:07
  • \$\begingroup\$ I specifically call out what I'm looking for. It doesn't need more or more "real" code. \$\endgroup\$ – Ryan Feb 14 '18 at 1:35
1
\$\begingroup\$

Lodash provides orderBy (formerly sortByOrder), as mentioned in this answer.

Something like this might work:

var dict_sorted = _.orderBy(dict, ['name'], ['asc']);

Edit:

Based on your comments, you don't want lodash at all. You just want to sort an array by an arbitrary key. The key, in this case, is the order in which a value appears in another array of values provided by you.

Array.sort() takes a comparison function as its argument. I'd suggest the following:

  1. Create a name -> integer mapping that will serve as the key. You will be passing an array, which is basically an integer->name mapping. So reverse it: name2index[name] = index (loop over all name/index pairs).

  2. Provide a function to compare the two "keys":

    function(a,b){ return name2index[a] - name2index[b]; }

  3. Figure out what should happen if a name is not present in the list you provided. Should unindexed values go to the top of the list? The bottom of the list? To /dev/null? Should the presence of such a value cause immediate failure?

\$\endgroup\$
  • \$\begingroup\$ I don't actually want to order by desc or asc, I want to order by a random array of values. If I specify ['random3', 'random2'] in an array, I want the object in the dictionary which corresponds to name: 'random3' to be first. \$\endgroup\$ – Ryan Feb 14 '18 at 0:50
  • \$\begingroup\$ I'm marking yours as correct, although I implemented something different before seeing your response. I decided to have the function take in the dictionary and the array, loop over the array and per each iteration do a lodash _.find for that value in my dictionary. If it exists, push it to an array I'll then return. I figure I have to define the order I want it ordered by in my code somewhere anyways. \$\endgroup\$ – Ryan Feb 14 '18 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.