The way to do this nowadays, as recommended by the Unicode standard, is to store text in logical order (good explanation here), which means the order in which it is read.
The OS knows that a specific string contains Hebrew by looking at the character codes. It applies the Unicode Bidirectional Algorithm to determine the correct display order. Typically an OS will do a quick scan of the string first to see if there are any right-to-left characters or control codes constraining the order. If not, the string doesn't need reordering.