Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

sorting issues in turn tracker

Hi, i've been banging my head trying to fix some issues with my script. Basically, it is not always sorting my turnorder properly. I found that its failing sometimes when i have a mix of single digit and double digit values.. I think, but not sure, that it might be related to int vs string pr values, maybe someone knows and can help, as I'm not very familiar with javascript. It seems when you modify the turn order via the input box, the values get applied as a string. Adding to the tracker via rolls always adds as an int. For example, here's one of my turn order objects: [{"id":"-1","pr":2,"custom":"Second Counter"},{"id":"-JLkUe86wm_J4sEtuNhp","pr":"5","custom":"","_pageid":"7F73B956-71C3-4C5B-8C72-576EE4F15EA4"},{"id":"-JLkUZC3ZFvLfKyye_ju","pr":18,"custom":"","_pageid":"7F73B956-71C3-4C5B-8C72-576EE4F15EA4"},{"id":"-JLkcaOb-TVK1IUhn4mQ","pr":"20","custom":"","_pageid":"7F73B956-71C3-4C5B-8C72-576EE4F15EA4"}] PR values for two of the items in the array seem to be strings while the others are integers. And here's the sort function i use on the turn order: function sortJSON(turnorder, pr) { return turnorder.sort(function(a, b) { var x = parseFloat(a[pr]); var y = parseFloat(b[pr]); return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }); } Any ideas? Thanks ahead of time.
1400479916
Lithl
Pro
Sheet Author
API Scripter
a[pr] and b[pr] is your problem. The script is assuming you have a variable named "pr" and it trying to use the value of that variable to index into a and b. However, there is no "pr" so the index is undefined. Try using a['pr'] and b['pr'], or else a.pr and b.pr.