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 ... |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
![]() Join Date: Jan 2009 Location: Your Imagination
Posts: 624
Blog Entries: 4 Thanks: 1
Thanked 22 Times in 21 Posts
![]() ![]() ![]() Expertise: PHP Experience: Professional |
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
}
]
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 )
__________________ Panthr - Web Design and Development |
| | |
| | #2 |
| Member Join Date: Jan 2009 Location: London, UK
Posts: 35
Thanks: 1
Thanked 1 Time in 1 Post
![]() Expertise: Web Development Experience: Professional |
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 :-) |
| | |
| | #3 |
![]() Join Date: Jan 2009 Location: Your Imagination
Posts: 624
Blog Entries: 4 Thanks: 1
Thanked 22 Times in 21 Posts
![]() ![]() ![]() Expertise: PHP Experience: Professional |
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.
__________________ Panthr - Web Design and Development |
| | |
| | #4 |
| Member Join Date: Jan 2009 Location: London, UK
Posts: 35
Thanks: 1
Thanked 1 Time in 1 Post
![]() Expertise: Web Development Experience: Professional |
It would have been a bit of a pain. But thankfully most things of that sort (no pun intended) have already been done... :)
|
| | |
![]() |
| Tags |
| array, sorting |
| Thread Tools | |
| Display Modes | |