Hello lovely people, So I am using an optional rule for 7th Edition Call of Cthulhu that allows the players to roll for their initiative and then they go according to the degree of success they scored. If a tie happens, person with the highest dexterity goes first. Now, I've written the first part of the custom initiative script and it works fine. Rolls for the player and determines degree of success and puts that in the tracker. on("ready",function() { on("chat:message",function(msg){ // CLEAR TURN TRACKER if(msg.type=="api" && (msg.content.indexOf("!CthulhuReset")==0)) { var turnorder = []; Campaign().set("turnorder", JSON.stringify(turnorder)); return; }; // CLEAR TURN TRACKER if(msg.type=="api" && (msg.content.indexOf("!CthulhuInit")==0 || msg.content.indexOf("!CthulhuInitBonus")==0)) { var selected = msg.selected; if (selected===undefined) { sendChat("API","Please select a character."); return; } var tok = getObj("graphic",selected[0]._id); var character = getObj("character",tok.get("represents")); if (character===undefined) { sendChat("API","Please add a name to this token."); return; } var RollOne = randomInteger(100); var RollTwo = randomInteger(100); if (msg.content.indexOf("!CthulhuInitBonus")==0) { var Rollsarr = [RollOne, RollTwo]; var playerRoll = Math.min.apply(Math, Rollsarr); } else { playerRoll = RollOne; } // var playerRoll = randomInteger(100); // var playerRollBonus = randomInteger(100); var playerDex = getAttrByName(character.id, "dex"); var InitDegree; if (playerRoll == 1) { InitDegree = "Critical"; } else if (playerRoll <= playerDex / 5) { InitDegree = "Extreme"; } else if (playerRoll <= playerDex / 2) { InitDegree = "Hard"; } else if (playerRoll <= playerDex) { InitDegree = "Success"; } else if (playerRoll == 100) { InitDegree = "Critical Fail"; } else if (playerRoll > playerDex) { InitDegree = "Fail"; } else { InitDegree = "Critical Fail"; } //TRY AND SEND CHAT VIA TEMPLATE if (msg.content.indexOf("!CthulhuInitBonus")==0) { sendChat(tok.get("name"), "&{template:coc-bonus} {{name=Initiative (Bonus Die)}} {{success=[["+ Math.floor(playerDex) +"]]}} {{hard=[["+ Math.floor(playerDex/2) +"]]}} {{extreme=[["+ Math.floor(playerDex/5) +"]]}} {{roll1=[["+ playerRoll +"]]}} {{PlayerRoll1=[["+ RollOne +"]]}} {{PlayerRoll2=[["+ RollTwo +"]]}}"); } else { sendChat(tok.get("name"), "&{template:coc-1} {{name=Initiative}} {{success=[["+ Math.floor(playerDex) +"]]}} {{hard=[["+ Math.floor(playerDex/2) +"]]}} {{extreme=[["+ Math.floor(playerDex/5) +"]]}} {{roll1=[["+ playerRoll +"]]}}"); } //END OF TRY AND SEND CHAT VIA TEMPLATE var turnorder; if(Campaign().get("turnorder") == "") turnorder = []; //NOTE: We check to make sure that the turnorder isn't just an empty string first. If it is treat it like an empty array. else turnorder = JSON.parse(Campaign().get("turnorder")); //Add a new custom entry to the end of the turn order. turnorder.push({ id: tok.get("id"), pr: InitDegree.toString(), custom: "" }); Campaign().set("turnorder", JSON.stringify(turnorder)); } }); }); Now I want to have a function that sorts the tracker in the following order, respecting highest dexterity for tie breaks. Critical Extreme Hard Success Fail Critical Fail Here's an example of my turn order now. So all the "Critical" scores should go first in order of initiative, then Extreme, then Hard, etc. Can you please help me with that part of the script? PS: I know my code isn't very pretty, but I am not a programmer, I did this by studying other code and reading online!