In Javascript, I need to order objects in an array according to type. Each type has a higher priority, so an object with a wipe type should have the highest priority, so it should be in front of the array (index = 0).
What would be the best way to sort these objects? Is there a built-in function that can do this?
For example,
function sortObjects( objs ) { // objs is an unsorted array of objects var animPriority = {"wipe": 1, "fly": 2, "iris": 3, "flip": 4, "cube": 5, "blur": 6, "zoom": 7, "fade": 8, "glow": 9, "rotate": 10}; for (var i=0; i<objs.length; i++) if (objs[i].type == "wipe") // bubblesort/bubbleswap element in objs[0] with objs[i]???? // a bubble sort doesn't seem efficient though? }
javascript sorting algorithm
Jake m
source share