'Sorting an array of objects based on a property value (int) [duplicate]

This question deals with my algorithm and why it isn't working. More specifically, I would like to know how it can be improved to do what I want it to do. That is why it is different from the suggested duplicate question.

I am trying to create a function that sorts an array of objects based on a property value (int) that they all share in common, "indexFound". As you may suspect, I am trying to place elements with a lower indexFound at the beginning of the array.

function organizeTokens(list) {
    for (i = 0; i < list.length - 1; i++) {
        if (list[i].indexFound < list[i + 1].indexFound) {
          // do nothing
        } else if (list[i].indexFound > list[i + 1].indexFound) {
          var tempVal = list[i];
          list[i] = list[i + 1];
          list[i + 1] = tempVal;
        } else {
        // should not happen unless we are comparing the same token
        }
    }
};

As it stands, this code is not making any difference when I feed it an array of objects. The elements are still not in the order that they should be. Am I approaching this in the right way? Am I missing something obvious?

EDIT: -------------------------------------------------------------------

Example input: organizeTokens([{value: "if", indexFound: 7}, {value: "a", indexFound: 0}])

Expected output: [{value: "a", indexFound: 0}, {value: "if", indexFound: 7}]

Actual output: [{value: "if", indexFound: 7}, {value: "a", indexFound: 0}]



Solution 1:[1]

You can use Array.prototype.sort() and define a compare function:

function compareIndexFound(a, b) {
  if (a.indexFound < b.indexFound) { return -1; }
  if (a.indexFound > b.indexFound) { return 1; }
  return 0;
}

list.sort(compareIndexFound);

Simpler/Concise version of above compare function:

function compareIndexFound(a, b) {
  return a.indexFound - b.indexFound;
}

Using ES6:

list.sort((a, b) => a.indexFound - b.indexFound);

You can define your own sortBy function:

function sortBy(arr, prop) {
  return arr.sort((a, b) => a[prop] - b[prop]);
}

sortBy(list, 'indexFound');

Solution 2:[2]

You can use JavaScript's built-in sort:

list.sort(function (l, r) {
    return l.indexFound - r.indexFound;
});

If you're using a utility like lodash or underscore, they have a sort function that's even simpler:

var sorted = _.sortBy(list, 'indexFound');

Example:

var list = [
    { indexFound: 9 },
    { indexFound: 3 },
    { indexFound: 17 },
    { indexFound: 1 }
];

var sorted = _.sortBy(list, 'indexFound');

console.log(sorted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Solution 3:[3]

Use the JS sort method with a custom callback function. Like this:

list.sort(function (a, b) {
    return a.indexFound - b.indexFound;
});

This will sort as ascending (lowest to highest).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3