Alternate implementation of key indexed sort (not for use when number of different keys is large)