best tutorials on sorting arrays?

J

Jake Barnes

Can anyone point me to good articles on sorting arrays? In particular,
with a two dimensional array, sorting by the rows of the second
dimenion? Dynamically, maybe?

I've got a an array of files, with their names and dates, created by
PHP, and once the page is downloaded, I'd like people to be able to
sort it dynamically.
 
V

VK

Jake said:
Can anyone point me to good articles on sorting arrays? In particular,
with a two dimensional array, sorting by the rows of the second
dimenion? Dynamically, maybe?

I've got a an array of files, with their names and dates, created by
PHP, and once the page is downloaded, I'd like people to be able to
sort it dynamically.

That was already such question before, but I failed to find the link.

Anyway, JavaScript array is not a real multi-dimentional array, it's
so-called jagged array: a sigle-dimention array having other arrays as
its elements. It means that you can use sort() method right on the top
dimention:

var files = [
['foo',5678],
['bar',1234]
]

function byDate(a,b) {
return a[1] - b[1];
}

var sortedByDate = files.sort(byDate);

This example presumes that you are using milliseconds for dates. But
you can adjust it for string dates either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top