function sortRanks(p1, p2) { var breakOrder = ['score', 'opponent_match_percentage', 'game_win_percentage', 'opponent_game_win_percentage', 'opponent_opponent_match_win_percentage', 'opponent_opponent_game_win_percentage']; for (i in breakOrder) { if (p1[breakOrder[i]] != p2[breakOrder[i]) { return ((p1[breakOrder[i]] > p2[breakOrder[i]]) ? -1 : 1); } } return -1; //tie }]]>

JC: I should have made clear that the incoming values are all three-decimal-point numbers, from 0.000 to 1.000, so the comparisons will work correctly.

The elimination of the leading zero is definitely a kludge, and should probably be replaced with

return Number(array.join(''));

which appears to do the right thing.

However, you’re right that it would be better to work with numbers entirely, eliminating the concatenation approach. Maybe something like:

function sortRanks(p1, p2) { var breakOrder = ['score', 'opponent_match_percentage', 'game_win_percentage', 'opponent_game_win_percentage', 'opponent_opponent_match_win_percentage', 'opponent_opponent_game_win_percentage']; function getNumber(p) { var num = 0; for (i in breakOrder) { num = num + ((p[breakOrder[i]] * 1000) * Math.pow(10,(breakOrder.length-i))); } return num } return (getNumber(p1) > getNumber(p2)) ? -1 : 1; }

If that works (I haven’t tested it), I’ll probably switch to it, as it looks like a cleaner method. Thanks for the comments!

]]>You’re generating a large number by performing string operations on OMP, GWP, OGWP, OOMWP, OOGWP, any of which can be 100, which will sort incorrectly. Any of those numbers can also be a single digit. Using that algorithum, a score of [6, 100, 9] (61,009) will score lower than [6, 99, 10] (69,910). You’ll also have similar problems with number of decimal places. Oops.

The “(array.join(”).charAt(0)==0)” is just a kludge for working with a string that’s not a number. Why not just work with numbers to begin with? Or if you prefer, just perform an alphanumeric sort.

]]>