I want to sort nodes of a certain parent node as they are parsed by XMLDOMParser.
The following is what I tried so far. It causes the image to hang and CPU to sweat. I've put half a day in - before I continue, what's the best way to do this? Am I on the right path? It seems very complicated ( I almost feel like I'm working with Morphic ;-) )
The parent node is XMLPageElement which is a subclass of XMLElement, in which I overrode #nodesClass to return XMLSortedList.
XMLSortedList is a subclass of XMLNodeList with the following overrides:
self nodes sortBlock: self sortBlock.
^ [ :a :b | a < b ].
^ super XMLSortedList.
There is also XMLSortedList class>>collectionClass
The above overrides XMLOrderedList's use of OrderedCollection. I refactored XMLOrderedList by pulling all references to OrderedCollection into this method.
> I did the simplest thing I could think of and overrode
> ^ self nodes sort: [ :a :b | a < b ].
> It worked but feels like a hack. Anyone have anything better?
Why do need to sort while the parser is parsing? Can't you sort just afterwards? I think this is the cleanest approach.
If you want to sort the elements immediately than you need custom classes like you wrote. Did you have a look at
? You can set a nodeFactory in the XMLDOMParser that should emit your classes while parsing. In your custom class there should be a possibility to override nodesClass in a clean way to get the list changes done. If not that would be a needed enhanced to the parser.
> Am 22.06.2011 um 05:11 schrieb Sean P. DeNigris:
>> I did the simplest thing I could think of and overrode
>> ^ self nodes sort: [ :a :b | a < b ].
>> It worked but feels like a hack. Anyone have anything better?
> Why do need to sort while the parser is parsing? Can't you sort just afterwards? I think this is the cleanest approach.
Yeah, my guess is that sorting a collection while it's being iterated upon is bad news.