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) {
for (var i = 0; i < objArray.length; i++) {
objArray = parseFloat(objArray[i]);
objArray.sort(key);
return objArray;
}
}
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.
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}].
@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?
I understand using the callback function in .sort but I get back .sort() is not a function.
Then you're not calling it on an array. You'll have to show your code.
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.
Not to forget returning 0 if the elements are equal.
a few small points for completeness:
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={}
objArray.sort( function (a, b, c) {
a=a.q; b=b.q;
if((c=cache[a+b])!==undefined){return c;}
if( a > b ) {
return cache[a+b]=1;
} else if( a < b ) {
return cache[a+b]=-1;
}
return cache[a+b]=0;
} );
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
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).
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).
what do you mean by "some sorting algorithms"?
im talking about
Code:
function srtStr(a,b){return a>b?1:-1;}
versus
Code:
function srtStr(a,b){return a>b?1:(b==a?0:-1);}
i also said "when sorting unique strings", which means that the difference is always NaN, unless you know something i don't...
after all, if the elements are unique, it's impossible for a proper sort function to return zero.
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%