Implemented sorting in steps.