Connecting Tech Pros Worldwide Help | Site Map

sorting an array

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 12:28 PM
Michael Hill
Guest
 
Posts: n/a
Default sorting an array

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


  #2  
Old July 20th, 2005, 12:28 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: sorting an array

Michael Hill <hillmw@ram.lmtas.lmco.com> writes:
[color=blue]
> If I have an array defined like this:
>
> var list = [
> ["1","359","No a Test xxxxxxxxxxxxxxx","
> 0","08/18/2003","Approved"],[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #3  
Old July 20th, 2005, 12:28 PM
Douglas Crockford
Guest
 
Posts: n/a
Default Re: sorting an array

> If I have an array defined like this:[color=blue]
>
> 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?[/color]

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
  #4  
Old July 20th, 2005, 12:28 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: sorting an array

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

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)
  #5  
Old July 20th, 2005, 12:28 PM
Douglas Crockford
Guest
 
Posts: n/a
Default Re: sorting an array

> > list.sort(function (a, b) {[color=blue][color=green]
> > if (a[2] == b[2]) {
> > return 0;
> > }
> > if (a[2] < b[2]) {
> > return -1;
> > }
> > return 1;
> > });
> >[/color]
>
> In short:
>
> list.sort(
> function(a,b) {
> return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
> }
> );[/color]

Or shorter,

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

  #6  
Old July 20th, 2005, 12:28 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: sorting an array

"Douglas Crockford" <nospam@covad.net> writes:
[color=blue]
> Or shorter,
>
> list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #7  
Old July 20th, 2005, 12:28 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: sorting an array

Douglas Crockford wrote on 01 dec 2003 in comp.lang.javascript:
[color=blue][color=green][color=darkred]
>> > list.sort(function (a, b) {
>> > if (a[2] == b[2]) {
>> > return 0;
>> > }
>> > if (a[2] < b[2]) {
>> > return -1;
>> > }
>> > return 1;
>> > });
>> >[/color]
>>
>> In short:
>>
>> list.sort(
>> function(a,b) {
>> return (a[2]>b[2])?1:(a[2]<b[2])?-1:0
>> }
>> );[/color]
>
> Or shorter,
>
> list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});[/color]

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)
  #8  
Old July 20th, 2005, 12:28 PM
Douglas Crockford
Guest
 
Posts: n/a
Default Re: sorting an array

> >> > list.sort(function (a, b) {[color=blue][color=green][color=darkred]
> >> > 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
> >> }
> >> );[/color]
> >
> > Or shorter,
> >
> > list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});[/color]
>
> Even shorter:
>
> list.sort(function(a,b){return (c=a[2]-b[2])?(c>0)?1:-1:0});[/color]

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

list.sort(function(a,b){return a[2] - b[2]});
  #9  
Old July 20th, 2005, 12:28 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: sorting an array

Lasse Reichstein Nielsen wrote on 02 dec 2003 in comp.lang.javascript:
[color=blue][color=green]
>> Or shorter,
>>
>> list.sort(function(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});[/color]
>
> Or even shorter, and much uglier,
> list.sort(function(a,b){return a[2]<b[2]?-1:+(a[2]>b[2]);});[/color]

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)
  #10  
Old July 20th, 2005, 12:29 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: sorting an array

"Evertjan." <exjxw.hannivoort@interxnl.net> writes:
[color=blue]
> list.sort(function(a,b){return +((c=a[2]-b[2])>0)-(c<0)});[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #11  
Old July 20th, 2005, 12:29 PM
Evertjan.
Guest
 
Posts: n/a
Default Re: sorting an array

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

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)
  #12  
Old July 20th, 2005, 12:30 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: sorting an array

"Evertjan." <exjxw.hannivoort@interxnl.net> writes:
[color=blue]
> So for strings, and without an if or ?: clause, we are stuck with:
>
> return +(a[2]<b[2])-(a[2]>b[2])[/color]

That's cute!
[color=blue]
> Internally machine coded in a comparison, the three possiblilities must be
> available as a "triary" and reduced to a binary output. Silly higher
> languages.[/color]

I miss Perl's <=> operator :)

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #13  
Old July 20th, 2005, 12:30 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default Re: sorting an array

Lasse Reichstein Nielsen <lrn@hotpop.com> writes:
[color=blue]
> "Evertjan." <exjxw.hannivoort@interxnl.net> writes:[/color]
[color=blue][color=green]
>> return +(a[2]<b[2])-(a[2]>b[2])[/color]
>
> That's cute![/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
  #14  
Old July 20th, 2005, 12:30 PM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: sorting an array

JRS: In article <Xns944563C16B7FDeejj99@194.109.133.29>, seen in
news:comp.lang.javascript, Evertjan. <exjxw.hannivoort@interxnl.net>
posted at Tue, 2 Dec 2003 08:48:22 :-
[color=blue]
>So for strings, and without an if or ?: clause, we are stuck with:
>
>return +(a[2]<b[2])-(a[2]>b[2])[/color]

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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.