sorting chunks before storing them in the db