working version sort