working sort method