Order of nodes on a way in OSM XML data

I have exported a map as OSM XML data. The nodes making up a way need to be ordered correctly for an application that I am building. In other words if I am traversing the way, the nodes need to be in the same order in the map data that they are in reality. The nodes in some of the ways in my XML data are ordered correctly, but others are not. Any ideas why?

If the latitude and/or longitude of each node making up a way always increase or decrease, simply sorting the data on latitude or longitude orders the nodes correctly, but quite obviously this does not always work. I can correct the incorrectly ordered nodes manually, but for a large map this will be incredibly time-consuming.


Can you post a bounding box with ways or a way id where this phenomenon happens? I cannot really image a way with a wrong node-order.

Have a look at the way in i.e. JOSM. The way should be displayed there, as is in the .osm-file.
You can also have a look on the way itself in the OSM-API: http://www.openstreetmap.org/browse/way/