By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,627 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,627 IT Pros & Developers. It's quick & easy.

How to sort the array like this ?

P: n/a
abs
ar = new Array()
ar = {"key1":"value1", "key2":"value2, "key3":"value3"};

Anybody knows how to sort such arrray by values ? I've tried writing the
comparing function and than
ar.sort(myCompareFunction)
but there appears an error: "ar.sort is not a function"

Best regards,
ABS
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On 20/03/2005 09:31, abs wrote:
ar = new Array()
This creates an empty array object.
ar = {"key1":"value1", "key2":"value2, "key3":"value3"};
This creates an initialised object, and overwrites the previously
assigned array.

You need to make up your mind: do you want an array, or an object?
Anybody knows how to sort such arrray by values ?


It doesn't make sense to sort an object which has non-numeric indicies
because you can't enumerate an object in any guaranteed order.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2

P: n/a
abs
Michael Winter wrote:
This creates an initialised object, and overwrites the previously
assigned array.
Oh, ok, now I know.
You need to make up your mind: do you want an array, or an object?
Array.
It doesn't make sense to sort an object which has non-numeric indicies
because you can't enumerate an object in any guaranteed order.


Understood. Thank you very much.

ABS
Jul 23 '05 #3

P: n/a
On 20/03/2005 10:22, abs wrote:
Michael Winter wrote:


[snip]
You need to make up your mind: do you want an array, or an object?


Array.


I forgot to mention, array literals use square brackets. However,
there is no way to specify the indicies:

var array = ['a', 'b', 'c', 'd'];
array[1] // 'b'

That said, you can skip indicies by omitting a value:

var array = ['a', 'b', , 'd', 'e'];

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a
> ar = new Array()
ar = {"key1":"value1", "key2":"value2, "key3":"value3"};
You are missing a close quote on "value2. It is a good idea to run
JSLint on sample code before posting. See http://www.JSLint.com
Anybody knows how to sort such arrray by values ? I've tried writing the
comparing function and than
ar.sort(myCompareFunction)
but there appears an error: "ar.sort is not a function"


You should only use arrays when all of the keys are whole numbers,
right? What you want is an object. Objects are unordered, but you can
access the members in sorted order.

<html><body><pre><script>
var ar, i, key, ob, va;
ob = {"key1":"value1", "key2":"value2", "key3":"value3"};

// Make an array of keys and an array of values.

ar = [];
va = [];
for (key in ob) {
ar.push(key);
va.push(ob[key]);
}

// Show sorted by key

ar.sort();
for (i = 0; i < ar.length; i += 1) {
key = ar[i];
document.writeln(key + ' : ' + ob[key] + ', ');
}

// Show sorted by value

va.sort();
for (i = 0; i < ar.length; i += 1) {
key = va[i];
document.writeln(va[i] + ', ');
}
</script></pre></body></html>

http://www.crockford.com/javascript/javascript.html
Jul 23 '05 #5

P: n/a
Michael Winter wrote:
On 20/03/2005 09:31, abs wrote:
ar = {"key1":"value1", "key2":"value2, "key3":"value3"};


It doesn't make sense to sort an object which has non-numeric indicies
because you can't enumerate an object in any guaranteed order.


Hey Mike!

Did you ever come across an object where the enumeration
order was not in the order of key creation (subject to
key removals, etc.)? In both PHP and Javascript I don't
recollect ever having run across such a situation, but
this is a point I've always been curious about.

Csaba Gabor from Vienna
Jul 23 '05 #6

P: n/a
On 20/03/2005 19:32, Csaba Gabor wrote:

[snip]
Did you ever come across an object where the enumeration order was
not in the order of key creation (subject to key removals, etc.)?


No, I haven't. However, I have access to a very limited subset of user
agents so that fact that I haven't means absolutely nothing.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7

P: n/a
> Did you ever come across an object where the enumeration
order was not in the order of key creation (subject to
key removals, etc.)? In both PHP and Javascript I don't
recollect ever having run across such a situation, but
this is a point I've always been curious about.


The ECMAScript Language Specification says of the object:

It is an unordered collection of properties each of which
contains a primitive value, object, or function. [4.3.3]

While an implementation might have so order (such as order of key
creation), there is no guarentee that the order is stable. Also,
programs that rely on a particular order will fail on other implementations.

http://www.crockford.com/javascript
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.