That is the effect of cleaning, yes. You have to remove the changes to the original lines (so they're not saved in your esp file, otherwise they count as new lines that overwrite the old ones). That's why it gives warnings when loading dialogue in TESCS, but the game doesn't give warnings and sorts it as intended.
Cyrano did a lot of research on this a while back... IIRC the last mod to load gets its dialogue first (or it could be the other way round... can't really remember... but it's predictable anyway). But if all mods are clean, the dialogue will be placed correctly with regard to the original lines.
Thanks melian for your responses. I did take a look at the MWSFD and some other dialogue posts, but please indulge me and let me know if I understand this correctly.
If I create 2 mods that insert new dialogue lines between original lines (O1 - O2 being original game dialogue lines, A1-A3 mods A's lines, and B1-B3 mod B's lines):
MOD A:
O1
A1
A2
A3
O2
...
MOD B:
O1
B1
B2
B3
O2
..
then clean both mods by deleting the changes made to O1 and O2 in each mod,
(thus O1 and O2 have no knowledge of the new dialogue lines, although A1.previous/B1.previous point to O1 and A3.next/B3.next point to O2)
and load both mods the game, due to whatever logic its engine employs, will correctly resolve and place the new lines of both mods in some sequential order between O1 and O2, i.e.:
O1
A1
A2
A3
B1
B2
B3
O2
...
I understand that the order I've given might be wrong, perhaps the B lines come before the A lines, but in either case the pointers of the O lines will be updated to point to the new lines, yes?
And in the above case, it's able to resolve the fact that originally A3.next pointed to O2 and B1.previous pointed to O1, after both mods load A3.next and B1.previous now point to each other?
If this is so, then I guess it works because even though the O lines have no knowledge of the new lines before the mods load (their pointers point to each other), any "clean" dialogue mod's first line will have a previous pointer back to an original line (O1) and it's last line will have a next pointer to an original line (O2).
I am somewhat surprised that the game is able to resolve this though. Not so much with just one mod, but what if you have 10 mods, all adding new dialogue lines between the same 2 original lines, thus all 10 have first and last lines that point to the same original lines.
Anyway, sorry if this sounds confusing and long, but the whole dialogue thing is still sort of mysterious to me.
Thanks