473,473 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to sort the array like this ?

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
7 2111
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
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
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
> 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
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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
3
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
2
by: Stefan Mueller | last post by:
With the PHP command 'sort' I can sort an array. However, it doesn't exactly sort the array I'd like to: <html> <body> <?php $my_array = array("AA", "11"); $my_array = array("AA", "22");...
1
by: Marcus Kwok | last post by:
I wrote a little test program to demonstrate what may be a misunderstanding on my part in the behavior of ArrayList::Sort(). I defined a class (Test) that has two data members: a System::String*...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: Santosh Nayak | last post by:
Hi, Is it possible to sort the array of struct based on the data members. e.g. struct { int a ; float b ; char c ; } TEMP ;
4
by: artev | last post by:
if I have one simple array 1 4 2 6 for to order the code is: arrayOrdin=arraySimple.sort(); and I have 1
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.