function sortByTextureOrderOrGroupThem (theArray, theOrder) {
var temp = [],
thisOrder = theOrder || [],
thisOrderLength = thisOrder.length,
thisOrderIndex,
order,
theArrayLength,
theArrayIndex,
element;
// if we were given an order then build the temp array from that and remove the element from theArray
for (thisOrderIndex = 0; thisOrderIndex < thisOrderLength; thisOrderIndex += 1) {
order = thisOrder[thisOrderIndex];
for (theArrayIndex = theArray.length - 1; theArrayIndex >= 0; theArrayIndex -= 1) {
element = theArray[theArrayIndex];
if (element.texture === order) {
temp.push(theArray.splice(theArrayIndex, 1)[0]);
}
}
}
// anything remaining in the array, group them together
theArray.sort(function (a, b) {
if (a.texture === b.texture) {
return 0;
}
if (a.texture < b.texture) {
return -1;
}
return 1;
});
// join any remaining grouped items to the temp
temp = temp.concat(theArray);
// empty theArray
theArray.length = 0;
// add the length and the starting index for use with apply
temp.unshift(temp.length);
temp.unshift(0);
// splice temp back into theArray
[].splice.apply(theArray, temp);
// return theArray in case it is needed this way
return theArray;
}
var texture1 = {"name": "texture1"},
texture2 = {"name": "texture2"},
texture3 = {"name": "texture3"},
meshes = [
{name: "Mesh 0", texture: texture1},
{name: "Mesh 1", texture: texture2},
{name: "Mesh 2", texture: texture3},
{name: "Mesh 3", texture: texture3},
{name: "Mesh 4", texture: texture2},
{name: "Mesh 5", texture: texture1},
{name: "Mesh 6", texture: texture1},
{name: "Mesh 7", texture: texture2},
{name: "Mesh 8",...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.