Implemented the stack sorting