2

I wrote a simple function to sort objects not expecting it to work but it does:

function sortObjs(objArr,field) {
    objArr.sort(
        function(a,b) {
            if( isNaN(a[field]) ) {
                return a[field].localeCompare(b[field]);
            } else {
                return parseFloat(a[field]) - parseFloat(b[field])
            }
        }
    );

    return objArr;
}

When I call this function I get my sorted objects without issue. But I was not expecting it to work because I thought the first two return statements would exit the function before it got to the last statement: return objArr.

3
  • 4
    They return from the inner function, not from sortObjs. Commented May 12, 2015 at 18:04
  • you can't cancel a sort() mid-way... (without throw) Commented May 12, 2015 at 18:09
  • What do you mean? I'm not trying to cancel a sort mid-way I'm checking if the property to sort on is a number or string before sorting. Am I doing something wrong? worked ok when I tried it. Commented May 12, 2015 at 18:13

2 Answers 2

3

You have a nested function. The first two returns will exit the inner function, while the last one will exit the outer one.

EDIT:

You can think of function returns as "replacing" the function with the returned value. For example:

var i = getSum(1, 3);

function getSum(a, b) {
    return a + b;
}

The function getSum returns the sum of a and b. The line var i = getSum(1, 3) will execute the lines of code contained in the function with a = 1 and b = 3. The value that is returned "replaces" the function call. So now the line of code looks like var i = 4;. Although this is not exactly how it works, it's a good way to conceptualize it. This is a special case because you aren't actually running the inner method here, you're passing it as a variable.

Let me know if you have any more questions!

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

2 Comments

Oh duh. That was obvious. I think I might ask another more general question about how closures work because I'm still a little confused. The value that is returned in the first return statements is returned to objArr?
@red888: No, the sort function takes a function as the parameter. The return values from the inner function return values to the sort function.
1

To understand why the inner function's return statements would not have an effect on the return statement in the outer scope, you need to understand how the Array.prototype.sort() function works.

The function arr.sort([compareFunction]), takes a function as a parameter.

compareFunction Optional. Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element.

  • The logic you write inside the compareFunction gets executed when the java script engine tries to compare two elements during its comparison operation.
  • Hence for each comparison that it makes, the function would return a value, based on which the elements would be ordered.
  • This implies that the compareFunction that we pass on as a parameter would be used to just obtain a value based on which two elements can be compared and not exit the sort operation, leave alone exiting the outer function.

Having said this, the code return objArr;, takes no effect, since the array would be sorted in place.

Comments

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.