I am trying to print out documents in various different orders. Right now I have a collection of print objects and an XML document where each node has attributes associated with a print object in the collection. I want to sort these objects by sorting the XML document based on these attributes.
Right now I am using a series of collections that i am cycling through and adding the node into a new collection based on the sorting i need done. The problem is 3 or 4 sorts in it gets messy. I am sure there has to be a better way to sort this collection of print objects, using XML or not.
Here are some attributes that my XML has that i am sorting on:
- # of pages
- Zip Code listed on document
- Name of Document
Does anyone have a better idea?
thanks,
EDIT –
my document will look something like
<xml>
<claim key="1" att1="wd" att2="de"/>
<claim key="2" att1="wd" att2="de"/>
<claim key="3" att1="wd" att2="de"/>
<claim key="4" att1="wd" att2="de"/>
</xml>
The key value is associated with a spot in the print object collection. The claim nodes may have more child nodes but i do not think that is relevant at this point. I would just like to be able to sort based on the attributes, there will be at least 3 attributes but more may be added. Also this needs to be versatile so if I need to switch the sorting order it should be somewhat easy to rearrange. Once I have the XML in the correct order I want, then i use a cycle through the XML in order putting the key into a list and then print out the Print objects by the keys in that list.
thanks,
I tried this example out in LINQPad and it seems to do what you’re asking.
It finds the
<claim>nodes in your XML, selects the attributes on which you’d like to sort, sorts them, and then returns anIEnumerable<XElement>collection representing the sorted<claim>nodes.You can then use the
queryresult to build yourPrintobjects, or you could change the above to have it do it for you:I think it fits the maintainability requirements you mentioned — you just have to change add/remove/change the attribute names in the LINQ query if you need to sort differently.
EDIT: To get the key into a list of
ints, as per your comment below: