I'm having trouble using a function within a Node.js module. I'm using a custom sort function to sort an array of objects by the value of a certain property.
exports.getResult = function(cards) {
cards.sort(sortByField('suit'));
...
...
...
return cards;
}
function sortByField(fieldName) {
return function(card1, card2) {
return card1[fieldName] < card2[fieldName]
};
}
When I use the getResult function NOT as an export, and call it from the same file, everything works as expected. The card object with the highest suit value is first and the card object with the lowest value is last.
However, when I call the function as an export from my index.js, the sort doesn't happen. There is no error, the rest of the function just executes with the array in the same order that it was before the sort function.
I've tried everything I can think of. I've done module.exports.sortByField(), I've tried requiring the .sortByField() in a completely separate module. I'm sure there's a simple answer, but I just can't figure it out.