working sort algo