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

sorting an array

P: n/a
If I have an array defined like this:

var list = [
["1","359","No a Test xxxxxxxxxxxxxxx","
0","08/18/2003","Approved"],
["2","268","test character fields for 500
","129","07/14/2003","Approved"],
["3","288","textarea filled XXXXXXXx","
0","07/15/2003","Approved"]
];

How would I sort it by the 3th column?

Mike

Jul 20 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Michael Hill <hi****@ram.lmtas.lmco.com> writes:
If I have an array defined like this:

var list = [
["1","359","No a Test xxxxxxxxxxxxxxx","
0","08/18/2003","Approved"],


Please avoid line breaks when posting code. The code, as posted, is
invalid.

Try this:
---
function compare(a,b) {
if (a<b) {return -1;}
if (a>b) {return 1;}
return 0;
}

var sortedList = list.sort(function(a,b){return compare(a[2],b[2]);});
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
> If I have an array defined like this:

var list = [
["1","359","No a Test xxxxxxxxxxxxxxx","
0","08/18/2003","Approved"],
["2","268","test character fields for 500
","129","07/14/2003","Approved"],
["3","288","textarea filled XXXXXXXx","
0","07/15/2003","Approved"]
];

How would I sort it by the 3th column?


You can pass a function to the array.sort(fn) method.

list.sort(function (a, b) {
if (a[2] == b[2]) {
return 0;
}
if (a[2] < b[2]) {
return -1;
}
return 1;
});

// 15.4.4.11

http://www.crockford.com/javascript/survey.html
Jul 20 '05 #3

P: n/a
Douglas Crockford wrote on 01 dec 2003 in comp.lang.javascript:
list.sort(function (a, b) {
if (a[2] == b[2]) {
return 0;
}
if (a[2] < b[2]) {
return -1;
}
return 1;
});


In short:

list.sort(
function(a,b) {
return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
}
);
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #4

P: n/a
> > list.sort(function (a, b) {
if (a[2] == b[2]) {
return 0;
}
if (a[2] < b[2]) {
return -1;
}
return 1;
});


In short:

list.sort(
function(a,b) {
return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
}
);


Or shorter,

list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});

Jul 20 '05 #5

P: n/a
"Douglas Crockford" <no****@covad.net> writes:
Or shorter,

list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Or even shorter, and much uglier,
list.sort(function(a,b){return a[2]<b[2]?-1:+(a[2]>b[2]);});

And the original was still by far the most readable.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

P: n/a
Douglas Crockford wrote on 01 dec 2003 in comp.lang.javascript:
> list.sort(function (a, b) {
> if (a[2] == b[2]) {
> return 0;
> }
> if (a[2] < b[2]) {
> return -1;
> }
> return 1;
> });
>


In short:

list.sort(
function(a,b) {
return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
}
);


Or shorter,

list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Even shorter:

list.sort(function(a,b){return (c=a[2]-b[2])?(c>0)?1:-1:0});

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #7

P: n/a
> >> > list.sort(function (a, b) {
> if (a[2] == b[2]) {
> return 0;
> }
> if (a[2] < b[2]) {
> return -1;
> }
> return 1;
> });
>

In short:

list.sort(
function(a,b) {
return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
}
);


Or shorter,

list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Even shorter:

list.sort(function(a,b){return (c=a[2]-b[2])?(c>0)?1:-1:0});


That assumes that the values are numbers. If they are, then

list.sort(function(a,b){return a[2] - b[2]});
Jul 20 '05 #8

P: n/a
Lasse Reichstein Nielsen wrote on 02 dec 2003 in comp.lang.javascript:
Or shorter,

list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Or even shorter, and much uglier,
list.sort(function(a,b){return a[2]<b[2]?-1:+(a[2]>b[2]);});


Haha:

list.sort(function(a,b){return +((c=a[2]-b[2])>0)-(c<0)});

Ugliness is a joy forever in the hand of the beholder.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #9

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
list.sort(function(a,b){return +((c=a[2]-b[2])>0)-(c<0)});


That requires a[2] and b[2] to be numbers. Comparison works for
strings too, and the original poster's example had strings at offset
2.

(And as it was pointed out, if they are numbers,
list.sort(function(a,b){return a[2]-b[2];});
is sufficient).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

P: n/a
Lasse Reichstein Nielsen wrote on 02 dec 2003 in comp.lang.javascript:
And as it was pointed out, if they are numbers,
list.sort(function(a,b){return a[2]-b[2];});
is sufficient


True.

Not according to the specs, but I do not find that very important.

==============

So for strings, and without an if or ?: clause, we are stuck with:

return +(a[2]<b[2])-(a[2]>b[2])

==============

Internally machine coded in a comparison, the three possiblilities must be
available as a "triary" and reduced to a binary output. Silly higher
languages.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #11

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
So for strings, and without an if or ?: clause, we are stuck with:

return +(a[2]<b[2])-(a[2]>b[2])
That's cute!
Internally machine coded in a comparison, the three possiblilities must be
available as a "triary" and reduced to a binary output. Silly higher
languages.


I miss Perl's <=> operator :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #12

P: n/a
Lasse Reichstein Nielsen <lr*@hotpop.com> writes:
"Evertjan." <ex**************@interxnl.net> writes:

return +(a[2]<b[2])-(a[2]>b[2])


That's cute!


It should actually be the other way around (to sort increasingly):
return (a[2]>b[2])-(a[2]<b[2]);
(and you can omit the +, since "-" converts both arguments to numbers)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #13

P: n/a
JRS: In article <Xn********************@194.109.133.29>, seen in
news:comp.lang.javascript, Evertjan. <ex**************@interxnl.net>
posted at Tue, 2 Dec 2003 08:48:22 :-
So for strings, and without an if or ?: clause, we are stuck with:

return +(a[2]<b[2])-(a[2]>b[2])


For those using W3's TIDY to check the Web page, and I recommend doing
so, that needs to be at least
return +(a[2]< b[2])-(a[2]>b[2])

in order not to risk confusing TIDY (if using the same version as I do).
The combination LessThan Letter seems to be taken as requiring a closing
GreaterThan, unless the traditional <!-- and --> are used.

Nicer:
return +( a[2] < b[2] ) - ( a[2] > b[2] )

However, I do not see how the + is needed.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.