Array sorting

This is a discussion on Array sorting within the Javascript forums, part of the Development category; I need a really odd method of sorting something. I have an array of objects. I need to sort the ...


Reply
 
LinkBack Thread Tools Display Modes
Old 28th June 2009, 05:50 PM   #1
supermod
 
CloudedVision's Avatar
 
Join Date: Jan 2009
Location: Your Imagination
Posts: 624
Blog Entries: 4
Thanks: 1
Thanked 22 Times in 21 Posts
CloudedVision has a spectacular aura aboutCloudedVision has a spectacular aura aboutCloudedVision has a spectacular aura about
Expertise: PHP
Experience: Professional
Default Array sorting

I need a really odd method of sorting something. I have an array of objects. I need to sort the array by a value in the object. I also need the ability to sort descending OR ascending. For example:

Code:
[
{
x:1,
y:2
},
{
x:2,
y:1
},
{
x:3,
y:3
}
]
Could get sorted ascending by y or descending by x.

I could no doubt make a function to do this myself, but no point in re-inventing the wheel. (In this case I'm not sure if the wheel exists )
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!twitter
Reply With Quote
Old 6th July 2009, 10:23 PM   #2
Member
 
spinal007's Avatar
 
Join Date: Jan 2009
Location: London, UK
Posts: 35
Thanks: 1
Thanked 1 Time in 1 Post
spinal007 is on a distinguished road
Expertise: Web Development
Experience: Professional
Default

You can pass in a custom function to change the behaviour of the Array object's sort method. Check this out:
JavaScript Sorting
...the example looks perfect to what you're trying to do.

PS.: Long time no see :-)
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!twitter
Reply With Quote
Old 6th July 2009, 10:49 PM   #3
supermod
 
CloudedVision's Avatar
 
Join Date: Jan 2009
Location: Your Imagination
Posts: 624
Blog Entries: 4
Thanks: 1
Thanked 22 Times in 21 Posts
CloudedVision has a spectacular aura aboutCloudedVision has a spectacular aura aboutCloudedVision has a spectacular aura about
Expertise: PHP
Experience: Professional
Default

Wow! Thanks spinal! That is exactly what I needed! I thought I would need to write a function to do it myself, which would have been a pain.
CloudedVision is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!twitter
Reply With Quote
Old 6th July 2009, 11:58 PM   #4
Member
 
spinal007's Avatar
 
Join Date: Jan 2009
Location: London, UK
Posts: 35
Thanks: 1
Thanked 1 Time in 1 Post
spinal007 is on a distinguished road
Expertise: Web Development
Experience: Professional
Default

It would have been a bit of a pain. But thankfully most things of that sort (no pun intended) have already been done... :)
spinal007 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!twitter
Reply With Quote
Reply

Tags
array, sorting

Thread Tools
Display Modes