So I'm having a slightly tricky issue...
I'm generating an XML file within a class I'm writing. Let's say this was the starting XML:
<base>
<container id="0">
<element type="Image" x="0" y"0" />
<element type="Image" x="100" y"0" />
<container/>
</base>
I want to add additional <element>
's. The first order of sorting is by "type", then "x", then "y". So if I add a new "type" of <element>
, let's say type "Text", I want Text to be inserted after any "Image" <element>
's.
For example:
<base>
<container id="0">
<element type="Image" x="0" y"0" />
<element type="Image" x="100" y"0" />
<element type="Text" x="200" y"100" />
<container/>
</base>
The basic idea is to keep the list sorted as I add more <element>
's to each <container>
... The numeric sorting is simple enough, but I can't figure out a clean way to sort alphabetically.
Suggestions are appreciated.
The only thing I can think of is to get the types into an Array. Add the "new type", sort and call indexOf()... the number SHOULD be the current position I should insert before? Feels kludgy.