Factor out numeric sorting and handle trailing garbage better