473,554 Members | 3,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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","text area filled XXXXXXXx","
0","07/15/2003","Approved "]
];

How would I sort it by the 3th column?

Mike

Jul 20 '05 #1
13 9576
Michael Hill <hi****@ram.lmt as.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(funct ion(a,b){return compare(a[2],b[2]);});
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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","text area 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(funct ion (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.javas cript:
list.sort(funct ion (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(funct ion (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(funct ion(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});

Jul 20 '05 #5
"Douglas Crockford" <no****@covad.n et> writes:
Or shorter,

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


Or even shorter, and much uglier,
list.sort(funct ion(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
Douglas Crockford wrote on 01 dec 2003 in comp.lang.javas cript:
> list.sort(funct ion (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(funct ion(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Even shorter:

list.sort(funct ion(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(funct ion (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(funct ion(a,b){return (a[2]>b[2])?1:(a[2]<b[2])?-1:0});


Even shorter:

list.sort(funct ion(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(funct ion(a,b){return a[2] - b[2]});
Jul 20 '05 #8
Lasse Reichstein Nielsen wrote on 02 dec 2003 in comp.lang.javas cript:
Or shorter,

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


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


Haha:

list.sort(funct ion(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(funct ion(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(funct ion(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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2973
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 an array) and I want to sort on Descending. This is displayed using print_r() function. Array
7
3243
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)) CType(local4, Short) = CType(src, Short)
3
6392
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 <stdio.h> void main(void) { char lines; int count = 0, i;
7
10074
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 C++) just doesn't explain things good at all and the tutorials I've viewed don't really explain least significant digit first sorting or show examples,...
0
3220
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 way... here is my code on how I did it to help anyone starting out get an idea of how to use virtual mode in ..NET 2.0 Imports...
7
4804
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want...
5
6674
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 requirement of programs. If you don't know the difference between a list and array don't worry about it. A list is just an array without a name. They...
1
7174
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 methods or syntax to a less experienced perl coder. I will post links to online resources you can read if necessary. Experienced perl coders might find...
5
3166
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 search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user...
5
4912
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link:...
0
7516
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7787
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7881
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5428
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2012
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 we have to send another system
1
1121
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
831
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.