Depth Sorting with javascript

yanomano's picture

I'am trying to re-order an array of points according to the z component for depth sorting with javascript. There is an easy way to to manage the order with array.sort(), but it seems more complicated with an array of arrays...

My idea was to apply the array.sort() only on the Z component and find a way to return an array of int[] that represents the order of the original array with indices. Then re-order the original array of point according to these indices.

Is there an easy way to return the original indices of the array re-ordered with array.sort() ?

Thanks for your help !

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

usefuldesign.au's picture
Re: Depth Sorting with javascript

If you can't take an index in the sorting array, how can you index back to the original structure?

Have to search through original array for matching value in "Z" then push the entire point to a new array structure.

in pseudo-code:
take point_struct (array object)
copy "Z" elements to depth_array (1 dimensional array object)
sort depth_array (far to near)
iterate i in depth_array {
   find depth_array[i] in point structure (allowing for duplicate values!)
   check it's index in point_struct
   push found point in point_struct to sorted_points
   }
publish sorted_points

I would like to know if array.sort can sort arrays though, that would be best. Got my own JS array woes at moment or would attempt the code.

usefuldesign.au's picture
Re: Depth Sorting with javascript

Posted a solution in a new thread so as not to jack this one with other talk. Uses method I described in pseudo-code.

http://kineme.net/forum/Discussion/DevelopingCompositions/Sortingarraysa...

yanomano's picture
Re: Depth Sorting with javascript

Thanks for the pseudo code usefuldesign ! it should work without hesitation :)

dust's picture
Re: Depth Sorting with javascript

does any one use qc's structure sort patch ?

usefuldesign.au's picture
Re: Depth Sorting with javascript

Dust, you're a genius!!

I have overlooked the sort structure patch. Looks powerful. I don't know if I ever used it to be honest, think not.

gtoledo3's picture
Re: Depth Sorting with javascript

Hah, don't feel bad about the structure sort. This one has come up before too!

usefuldesign.au's picture
Re: Depth Sorting with javascript

Thanks George, you're a genius too btw.

Two in one day is that allowed?

dust's picture
Re: Depth Sorting with javascript

haha thanks for the comment. actually your not the first to mention that but yeah i overlooked the structure sort patch for a while. i tired it the other day and its wicked fast. much faster than making temp arrays and variables and doing triple nested loops in java script to swap and sort an arrays or well at least for this thread on depth sorting.

i did however notice the structure sorter doesn't work right unless you have declared the data types your sorting meaning it will not virtually sort a mixed structure for you. if you want to sort your keys just make a temp structure and store the keys as the values and the values as keys and sort that out. or at least thats how i have been sorting lately.

had to learn bubble and binary sorting etc for c++ class last year. kind of pain if you ask me. after we learned sorting the teacher says oh yeah there is built in sorting etc.. she did the same thing with binary dec and hex conversions. like you need to know how to do this by hand for the test but in reality you will never have to do this.

toneburst's picture
Re: Depth Sorting with javascript

I had to do bubble-sorting of z-coordinates in a Director project once, in about 1998. Not that it has proved in any way useful in the years since then.

a|x