![]() |
Javascript sorting
First of all could someone explain the difference between
var matchingGame = {}; and var matchingGame = []; Which of the following is the correct sorting of these 2 ? If compareFunction(a, b) is less than 0, sort a to a lower index than b. or If compareFunction(a, b) is less than 0, sort b to a lower index than a. I have found different sources and both of them show different answer https://developer.mozilla.org/en-US/...cts/Array/sort Quote:
http://www.wyodor.net/Public/JavaScr...0reference.pdf Quote:
|
Are you sorting numbers only or will some elements be some form of strings?
|
The two quotes are in effect the same.
|
Not to ask silly questions, but...Why didn't you just try it?
Code:
<script type="text/javascript"> |
Quote:
quote 1: If compareFunction(a, b) is less than 0, sort a to a lower index than b. quote 2: If compareFunction(a, b)is less than zero, sort b to a lower index than a. I am trying real hard to see your viewpoint, Philip, but I can't. I don't know how the two could be more diametrically opposed. |
Quote:
{ } is used to introduce a set of name/value pairs. As in: Code:
var sounds = {Also note that the above is entirely equivalent to doing: Code:
var sounds = new Object();Code:
sounds.cat************* The square bracket notation is used to create an array of elements (or to declare that a variable is an empty array, if there are no values between the brackets). Code:
var arr = [ 7, 3, 21, 911 ];Code:
alert( arr2[1] ); // shows pi |
Quote:
I'd have expected the Mozilla site to have the correct description for sort without needing to test it - they would be the official standard site for JavaScript if they hadn't given that responsibility to ECMA. |
Quote:
That's me sticking my tongue out at you. It was an *EXAMPLE*. A very very SILLY example. I only wanted to make the point that arrays are heterogenous in JavaScript. Quote:
I'm constantly amazed that people will ask a question in forums where they may have to wait hours or days for an answer when they could just try a smidgen of code themselves and know the answer in minutes, at most. |
One point would be that different browsers COULD in theory implement the function differently, but that'd be really silly. Other than that I couldn't agree more. I dont even answer such questions and if i do i just tell them to try it. If you give them the answer they'll just ask again next time and not learn anything.
|
Quote:
|
| All times are GMT +1. The time now is 05:04 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.