Question over Json sorting
http://www.devcurry.com/2010/05/sorting-json-array.html
I came across this article, i want few explantions over this article.
function SortByName(x,y) {
return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name开发者_如何学Go) ? 1 : -1 ));
}
arr.sort(SortByName);
What would be the arguements for arr.sort(x,y). What will be passed to the function from the JSON Object.
What does the SortByName function return. I don't understand the entire line. can anyone give me more details over this.
What would be the arguements for arr.sort(x,y).
The two values in the array that are currently being compared
What does the SortByName function return
0, -1 or 1 depending on which of the two Name properties was bigger.
You should probably read the documentation for sort
The argument of sort is a function that will be called several times with two different elements of the array, and will return 0 if the elements are equal, 1 if x > y and -1 if y > x.
return ((x.Name == y.Name) ? 0 : ((x.Name > y.Name) ? 1 : -1 ));
is the very same as:
if (x.Name == y.Name)
return 0;
else
if (x.Name > y.Name)
return 1;
else
return -1;
精彩评论