Add a generic sorting function

Signed-off-by: Timothy Gu <timothygu99@gmail.com>