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!
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!
Share Improve this question edited Nov 19, 2014 at 17:07 kurast 1,6153 gold badges17 silver badges41 bronze badges asked Jan 19, 2012 at 15:36 Chris YeungChris Yeung 2,7236 gold badges37 silver badges61 bronze badges9 Answers
Reset to default 4You 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 ));
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 . 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
"
- Sorting function which explains how two items will be sorted when pared to eachother
a return value of greater than one will flip the order of the items
part of the ternary operator
part of the ternary operator
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;}
}
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 pare both type and value, not just value.
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 parison 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;
}
}
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;
}
}
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 e before y;
if 1(or pos) is returned, x will e 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.