Implementing grad for sort op and all necessary things to make it work.