I have a list posts by tag, but the relation is not relevant because first look for the posts with the first tags stored alphabetically, and not the first main tag, that is to say for me the order of the tags are key because the first tag is the most relevant. So when saving everything, it is sorted alphabetically and this does not help me, any idea how to alter this?
Terms/Tags aren't ordered.
There is no order, only the order you give them. Fundamentally terms have no order. Any order you see in the editor is purely circumstantial, there is no order mechanism in the database for terms.
If you want to give special meaning to a term, you need to do so explicitly, e.g. store the term ID of the significant term in post meta. You cannot rely on order, as terms do not have order.
I faced the same issue and saw that while there is the
wp_term_relationships table, it doesn't get properly updated with the tags' order on post update. As a workaround, I hooked into the
rest_after_insert_this-post_type to update the order myself. Here is a more detailed explanation of what I did, along with some code that seems to work fine so far.