When it comes to the rearranged code that you are seeing is there any effect from doing this? If the attributes for the tag are specified in a different order does it cause your pages or code to not work? I'm not sure why the attributes order seems to be getting reversed but I'm more curious about the effect that this is having on your code.
Also, do you know which way it is being stored in the DB? This will give a clue about when and where the reformatting is occurring.