ticket7. Fix sort bugs. Add test