3

Code

function SortByID(x,y) {
    return x.id - y.id;
}

function SortByName(x,y) {
    return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));
}

Problem Description

I am new to Javascript and I am learning how to make a sorting algorithm. I have a few questions regarding the two functions above.

1.Can you please explain the line of code below to me in words?

return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));

2.What does the question mark in the above code mean?

3.what doest the "? 1: -1" and "? 0 :" means?

Many Thanks!

9 Answers 9

4

You are looking at a ternary operator. It's a short way of writing if else statements.

((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));

Is the same thing as.

if ( x.Name == y.Name ) {
    return 0;
} else {
    if ( x.Name > y.Name ) {
        return 1;
    } else {
        return -1;
    }
}

Another way to read it would be like this.

(( Condition ) [IF TRUE] 0 [IF FALSE] (( Condition ) [IF TRUE] 1 [IF FALSE] -1 ));
Sign up to request clarification or add additional context in comments.

2 Comments

It’s not exactly like an if statement — it’s important to understand that t ternary operator evaluates to a value — so you can use it anywhere, even when you’re not returning something.
This is very true. However, you have to return them to something whether it's a variable or return statement...which was confusing at first. It definitely takes some time to get used to.
1

The question mark is known as the conditional operator (or ternary operator) it is a shorthand way of an if then else block, bearing this in mind we have:

1.Can you please explain the line of code below to me in words?

If x.Name equals y.Name return 0 otherwise if x.Name is greater than y.Name return 1 otherwise return -1

2.What does the question mark in the above code mean?

The '?' operator takes the form:

(expression evaluating to true / false) ? (if true expression) : (if false expression)

3.what doest the "? 1: -1" and "? 0 :" means?

See answer 2 for syntactic meaning. In this context returning 0 means the values are deemed equal, -1 means the first value is less than the second and 1 means that the first value is greater than the second.

1 Comment

check your final return; should be -1 not 0
1

1 . That means literally:

    if(x.Name == y.Name) return 0;
    else{
      if (x.Name > y.Name) return 1;
      else return -1;
    }

2. and 3. The key is in ternary operator syntax. Expression A ? B : C means "check if A is true and if it is true then expression value is B otherwise C"

Comments

0
  1. Sorting function which explains how two items will be sorted when compared to eachother

a return value of greater than one will flip the order of the items

  1. part of the ternary operator

  2. part of the ternary operator

Comments

0

This type of statement is found in other programming language. It is shorter version of the if else statement. For example

if(condition){
statement1} else {
statement2}

can be written as

condition ? statment1 : statement2

In your case, two condition are being tested and the equivalent if else statement would be

    if(x.Name == y.Name) {
    return 0;
} else {
     if(x.Name > y.Name){
    return 1;}
    else {
    return -1;}
    }

Comments

0
return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));

If x.Name is the same as y.Name, return 0; otherwise, if x.Name is greater than y.Name, return 1; if both those statements are false, finally return -1.

Bonus note: use === instead of == so that you compare both type and value, not just value.

Comments

0

This makes heavy use of the ternary operator. The ternary operator is like a if statement in one line.

expression ? result if true : result if false

So that

3 == 3 ? "yes" : "no"

Returns "yes". Now basically this is a sort function. Sort functions want you to give them a number that represents a comparison between two items. If the items are equivalent, you return 0. If not, you return 1 or -1 to determine which one is bigger.

This expression basically checks if the names are equal. If they are, it returns zero. Otherwise, there is another condition which will return 1 if x.Name is "bigger" than y.Name, or -1 otherwise.

This could be expressed as the following:

if (x.Name == y.Name) {
    return 0;
}
else {
    if (x.Name > y.Name) {
        return 1;
    }
    else {
        return -1;
    }
}

Comments

0
  return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));

In other words

 if(x.Name == y.Name){
      return 0;
 }
 else{
    if(x.Name > y.Name){
        return 1;
    }
    else{
        return -1;
     }   
 }

Comments

0

I found a good site to explain these syntax :) Sorting Tutorial

Thanks for the answers above.

These functions are for the Javascript's inbuilt .sort() function to sort accordingly.

if you have:

var obj_to_sort; //in json format

Then we can simply sort this variable by entering:

obj_to_sort.sort(SortByName); //or
obj_to_sort.sort(SortByID); ...etc

and for each SortByXX function (which is declared by yourself), it should take in two parameters like below:

function SortByID(x,y) {
        return x.id - y.id;
}

function SortByName(x,y) {
    return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));
}

There are three scenarios:

if -1(or neg) is returned, x will come before y;

if 1(or pos) is returned, x will come after y;

if 0 is returned, x = y;

And by using .sort() function, it will automatically sort according to your algorithm.

and for:

 return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name)? 1: -1));

it means:

  • if x = y ; output 0
  • otherwise, if x > y ; output 1
  • otherwise, output -1.

? means output, : means otherwise.

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.