If I have several divs:
<div data-sort='1'>div1</div>
<div data-sort='4'>div4</div>
<div data-sort='8'>div8</div>
<div data-sort='12'>div12</div>
<div data-sort='19'>div19</div>
And I dynamically create the divs:
<div data-sort='14'>div1</div>
<div data-sort='6'>div1</div>
<div data-sort='9'>div1</div>
How can I get them to just sort into the divs already loaded in order, without having to reload all of the divs?
I think that I would need to build an array of the data-sort values of all of the divs on the screen, and then see where the new divs fit in,, but I am not sure if this is the best way.
Any help is appreciated
I made this into a jQuery function:
So you have a big div like "#boo" and all your little divs inside of there:
You need the "? 1 : -1" because of a bug in Chrome, without this it won't sort more than 10 divs! http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-Wrong.html
Answered the same question here:
To repost:
After searching through many solutions I decided to blog about how to sort in jquery. In summary, steps to sort jquery "array-like" objects by data attribute...
Html
Plain jquery selector
Lets sort this by data-order
See how getSorted() works.
Hope this helps!
I used this to sort a gallery of images where the sort array would be altered by an ajax call. Hopefully it can be useful to someone.
Fiddle: http://jsfiddle.net/ruys9ksg/
use this function
you can call this function just after adding new divs