![]() |
sorting through objects in an array
I'm trying to sort an array of objects with this function. objArray = [{q:2}, {q:1}, {q:3}] with a key of "q". so that it will return [{q:1}, {q:2}, {q:3}].
heres what I've got so far, I'm not sure if my logic is the one thing thats messed up or if I'm not using the correct parameter for the .sort method. please help! please no regexp or no answers just a push in the right direction please and thank you. Code:
function sortingTime(objArray, key) { |
You array initialization is incorrect.
See the following test to demonstrate the problems and the comments about them. Note: This does not address the sort problem because your initial premiss is wrong. Code:
<!DOCTYPE html> |
The other thing you need to know is that
objArray.sort(key); will sort the entire array with key being the comparison function used to determine the order to sort two entries into. You need to define that function to take two parameters and return a positive or negative value based on whether the entries need to be swapped or not. |
@ jmrker
Maybe I'm overlooking something, but the OP wrote Code:
objArray = [{q:2}, {q:1}, {q:3}]@ felgall Not to forget returning 0 if the elements are equal. @ OP This is how it can be done. Code:
var objArray = [{q:2}, {q:1}, {q:3}]; |
Quote:
Code:
objArray = [{q:2}, {q:1}, {q:3}]; |
@airblader I did something like yours, but at the same time I'm trying to pass the key parameter in the main function which is what will be sorting it by. I understand using the callback function in .sort but I get back .sort() is not a function. Any help clearing up this mystery for me?
would I pass in the key parameter from the main function into the callback function by chance? |
Quote:
I think you want to be able to specify the sorting key, in which case you can write a function to do it. You cant pass parameters to the callback function, that is what sort() does. Code:
objArray = [ {q:2, r:6}, {q:1, r:2}, {q:3, r:-5}, {q:9.7, r:-1}, {q:-4, r:0} ]; |
still getting .sort is not a function...
Code:
function sortingTime(objArray, key) { |
[QUOTE=JonBMN;1316606]
Quote:
|
what do you mean the callback function wouldn't work anyway?
|
Quote:
1. if you are comparing all numbers, subtracting one from the other will return 0 on a match, so the explicit zero-compare is not needed. 2. if your array elements are all unique strings, you don't need a zero-compare. Since string sorting is really slow, chopping off half the workload can make a big diff on how fast the sort performs. 3. going a bit more advanced, if you have a large array with many elements but few unique values, you might want to consider caching the sort-compare result. i've seen this pattern take a sort from 800ms to 35ms. something LIKE this: Code:
var cache={} |
*I've figured it out! thank god. but here is the solution I came too for the .sort.........
Code:
return objArray.sort(function(a, b) { |
Quote:
Code:
return objArray.sort(function(a, b) |
@ rndme
1. True 2. Not entirely -- some sorting algorithms will compare an element to itself. Your sorting function should always support this case (which just using the difference does, of course). |
Quote:
im talking about Code:
function srtStr(a,b){return a>b?1:-1;}Code:
function srtStr(a,b){return a>b?1:(b==a?0:-1);}after all, if the elements are unique, it's impossible for a proper sort function to return zero. |
| All times are GMT +1. The time now is 09:30 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.