This thing reminds me of Tetris. It is very simple to implement. All the block elements in the container are arranged with absolute positioning. If they can fit, put them here. If they cannot fit, find the ones that can fit in the queue. Element placement, if
cannot be found, then wrap it to the next line. The specific idea is this. There are detailed comments in the code, so just look at the code.
The following is a demo:
http://demo.jb51.net/js/2012/sortRect/
Code package downloadsortRect.rar