2

I'm trying to sort an array of objects, which is huge.

I have created the following functions. I have a compare function that looks like this:

  WordCounter.prototype.compare = function(a, b) {
    if (Object.values(a) < Object.values(b)) {
      return -1;
    }
    if (Object.values(a) > Object.values(b)) {
      return 1;
    }
    return 0;
  };

I then reference it in this function:

WordCounter.prototype.sortArray = function(array) {

    this.sortedArray = array.sort(this.compare);
  };

I then iterate over the array using the below function, it does sort the objects a bit but it seems to do it in chunks:

  WordCounter.prototype.returnWords = function(array) {
    for (var key in array) {
      return array[key];
    }
  };

Here is some dummy data for reference, there are actually thousands of objects in the array though:

[{tongues: 1}, {unpleasant: 50}, {sneak: 13}, {marched: 12}, {flaming: 2}]

I need to sort it like this:

[{unpleasant: 50}, {sneak: 13}, {marched: 12}, {flaming: 2}, {tongues: 1}]

Any idea why it is only sorting a little bit and not the whole array of hashes?

11
  • 1
    returning inside a for loop is a bad idea: it will stop the for loop immediately. Please provide some sample data and the desired output. Commented Jun 11, 2017 at 19:53
  • oh yes, thanks! - changed it to console.log for now Commented Jun 11, 2017 at 19:53
  • 1
    Also Object.values(a) < Object.values(b) looks suspicious: values returns an array ... What do you expect the comparison to do with arrays? Commented Jun 11, 2017 at 19:55
  • Hmm I see, I'm trying to sort the array by the values in each function. Do you know how I can access these when the keys are all differently named? Commented Jun 11, 2017 at 19:56
  • @Freddy based on which object property do you want to sort the array? Can you provide a sample of a couple of objects in your array? Commented Jun 11, 2017 at 19:58

1 Answer 1

2

You can use a helper function getValue to get a value from an object, assuming it has just one property, and sort by that:

function getValue(obj) {
    for (let key in obj) return obj[key]; // return any value found in the object
}

// Sample data
var arr = [{tongues: 1}, {unpleasant: 50}, {sneak: 13}, {marched: 12}, {flaming: 2}];

// Sort it
arr.sort((a, b)  => getValue(b) - getValue(a));

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

4 Comments

Works perfectly. Thank you!
Concise & clever
@trincot, do you know, which of the access of the first key is faster, Object.keys, for in or an iterator?
In this particular case it does not matter much, since the object is expected to have only one enumerable, own property (and no inherited ones), but if an object would have a multitude of enumerable, own properties, for..in (or any other way to consume the iterator) will be much faster. Object.keys will have to retrieve all properties, while only one is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.