Slick way to sort DIV positions?

L

laredotornado

Hi,

I have ten DIVs on my page, with IDs "e1" ... "e10". These DIVs can
get moved around on the screen in the course of the page interaction.
However, at a certain point, I would like to be able to sort where the
DIVs are currently positioned, using the top left coordinate as the
reference point, sorting from lowest "x" position to highest, and in
the case of a tie, sorting from lowest "y" position to highest.

Any ideas on an efficient way to do this?

Thanks, - Dave
 
P

pcx99

Hi,

I have ten DIVs on my page, with IDs "e1" ... "e10". These DIVs can
get moved around on the screen in the course of the page interaction.
However, at a certain point, I would like to be able to sort where the
DIVs are currently positioned, using the top left coordinate as the
reference point, sorting from lowest "x" position to highest, and in
the case of a tie, sorting from lowest "y" position to highest.

Any ideas on an efficient way to do this?

Thanks, - Dave

http://en.wikipedia.org/wiki/Quicksort
 
L

laredotornado

Thanks, but could you provide a code sample? The wikipedia article
has pseudocode but that doesn't work in Javascript. Also, where in
the article does it discuss getting coordinates of DIVs?

Thanks, - Dave
 
P

pcx99

Thanks, but could you provide a code sample?


sortArray = Array(5,8,9,10,38,81,24,83,22,21,1,4,63,73,42,88);

function qSort(sortArray, left, right)
{
var scratch_left = left; // remember current left
var scratch_right = right; // remember current right
var pivot = sortArray
; // remember current value
while (left < right) {
while ((sortArray
>= pivot) && (left < right)) {
right--;
}
if (left != right) {
sortArray
= sortArray
;
left++;
}
while ((sortArray
<= pivot) && (left < right)) {
left++;
}
if (left != right) {
sortArray
= sortArray
;
right--;
}
}
sortArray
= pivot;
pivot = left;
left = scratch_left;
right = scratch_right;

if (left < pivot) { qSort(sortArray, left, pivot-1); }
if (right > pivot) { qSort(sortArray, pivot+1, right); }
}

qSort(sortArray, 0, sortArray.length-1);​
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,688
Members
48,796
Latest member
Greg L.

Latest Threads

Top