I have an array of small arrays. The small arrays are IDs to objects that belong together.
faces = [[0,1,2,3],[4,5,6,7],[0,3,7,4],[1,2,5,6],[0,1,5,4],[2,3,7,6]];
I want to sort the array faces based on a calculation of the values inside the smaller arrays. Each value represent a z-value.
The Setup:
- I have an array of objects named objects.
- Each object has an array of vectors named vectors.
- Each vector has three coordinates: x,y,z.
- Each object also has an array called faces.
- Faces is an array of smaller array of IDs for the vectors. Each small array is a cluster of vectors.
What I want to do is to sort the array faces based on the average value of the z-value that are represented inside the small arrays.
I'm basing my try on this:
myArray.sort(function(a,b){
return b-a;
})
This is what I have so far in terms of sorting the objects[i].faces array:
objects[i].faces.sort(function(a,b){
var value1=0;
var value2=0;
for (var j = 0; j<objects[i].faces[a].length; j++) {
value1+=objects[i].vectors[faces[a][j]].z;
}
for (var j = 0; j<objects[i].faces[b].length; j++) {
value2+=objects[i].vectors[faces[b][j]].z;
}
return value1/objects[i].faces[a].length-value2/objects[i].faces[b].length;
})
It's currently complaining that he can't read the property "length" of undefined.
vectorsas well as the expected output?aandbalready refer to an array infaces.