The fact that there is a way to automatically detect the natural direction of text flow is what is causing you trouble: the bidirectional algorithm is not perfect. Sometimes you need to give it some extra hints, as explained in Unicode controls vs. markup for bidi support.
The Unicode bidirectional algorithm determines the directionality of text on the basis of the directional properties of each character. Occasionally the algorithm needs a little help to determine the flow of objects in the text that mixes Arabic or Hebrew characters with those of other scripts. In other cases you might want to override the effect of the bidirectional algorithm altogether.
You can fix this manually by adding U+202A ʟᴇғᴛ-ᴛᴏ-ʀɪɢʜᴛ ᴇᴍʙᴇᴅᴅɪɴɢ and U+202C ᴘᴏᴘ ᴅɪʀᴇᴄᴛɪᴏɴᴀʟ ғᴏʀᴍᴀᴛᴛɪɴɢ characters to the text, as I did below:
اُردو کا بعض اوقات ہندی کے ساتھ موازنہ کیا جاتا ہے. اُردو اور ہندی میں بُنیادی فرق یہ ہے کہ اُردو نستعلیق رسم الخط میں لکھی جاتی ہے اور عربی و فارسی الفاظ استعمال کرتی ہے. جبکہ ہندی دیوناگری رسم الخط میں لکھی جاتی ہے اور سنسکرت الفاظ زیادہ استعمال کرتی ہے. کچھ ماہرینِ لسانیات اُردو اور ہندی کو ایک ہی زبان کی دو معیاری صورتیں گردانتے ہیں. تاہم، دوسرے اِن کو معاش اللسانی تفرّقات کی بنیاد پر الگ سمجھتے ہیں۔ بلکہ حقیقت یہ ہے کہ ہندی ، اُردو سے نکلی۔اسی طرح اگر اردو اور ھندی زبان کو ایک سمجھا جاۓ تو یہ دنیا کی چوتھی (4th) بڑی زبان ہے۔
...‪(4th)‬...
You can also use the <bdi>
element in HTML5, or a <span>
in HTML4.
...<bdi dir="ltr">(4th)</bdi>...
...<span dir="ltr">(4th)</span>...
For more info about the bidirectional algorithm see What you need to know about the bidi algorithm and inline markup.
If this text comes from the user, I guess there is nothing you can do about it. I would just treat it as any other kind of faulty user input; like a typo.