added implementation for the sorting