added sorting function and tests