You'd do that by getting the elements, sorting the collection by z-index, and then iterate and use the index to set the new z-index. This does not affect the elements position in the DOM, we're only rearranging the collection.
$('[id^="div"]').sort(function(a, b) {
return a.style.zIndex - b.style.zIndex;
}).css('z-index', function(i) {return i+1;});
Note that you have to close the DIV elements to make this work