comparison function for terms