added sort function (fixed a bug with id's)