implement equivalent sorts

Very crude first implementation. Some tests are expected to fail. Not ready for merge.

@yilongli @andreistefanescu can you have a look?
This looks too expensive to me. Can you precompute some information and eliminate this nested loop? For example, have one representative for each equivalent class.