473,408 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

Jul 20 '05 #1
13 9571
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
> 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
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
> > 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
"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
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
> >> > 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
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
"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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.