I'm trying to insert div containers with class names into a box with other containers by defining class with numbers.
I'm able to get the following to work correctly as long as the markup is already in chronoligical order:
http://play.meyouand.us/140418-rearrange/rearrange4a.html
However if the markup order is in a mixed or reversed chronological order, the function doesn't position the div containers in the exact position (because the function I'm using is before()):
http://play.meyouand.us/140418-rearrange/rearrange4b.html
I'm currently stuck on how to solve this because I'm not sure if I should use the strategy of pre-sorting the boxes first, or if there's an existing jQuery function that can just place the div containers exactly where I need them which would make it an ideal and simple solution. Any thoughts on strategy or methodology will be helpful!
jQuery so far... - http://jsfiddle.net/foomarks/qM27z/2/
$('[class*=order-]').each(function() {
/* 1. Split the classes to get an array */
var cl = $(this).prop('class').split(/\s+/);
var clNumber = cl.map( function(val){
if (val.indexOf('order-') !== -1) { //find the match
return val.replace( /^\D+/g, '') // return back the number
}
});
console.log("clNumber: " + clNumber[1]);
/* 2. Presort boxes */
/* Strategy A
. If this clNumber is greater than the next .order- box, append it after
. else do nothing
*/
/* Strategy B
. Sort the array
. then .append() the output
. this may not work within the .each function because of sequencing
*/
/* 3. Use the insert number to reposition */
$(this).insertBefore('.box:nth-child('+ clNumber[1] + ')');
});