Can’t get list sorted by date. How to show data ordered by date?
XDocument doc = XDocument.Load(Server.MapPath("file.xml"));
IEnumerable<XElement> items = from item in doc.Descendants("item")
orderby Convert.ToDateTime(item.Attribute("lastChanges").Value) descending
where item.Attribute("x").Value == 1
select item;
Repeater1.DataSource = items;
Repeater1.DataBind();
Xml file looks like this:
<root>
<items>
<item id="1" lastChanges="15-05-2010" x="0" />
<item id="2" lastChanges="16-05-2010" x="1" />
<item id="3" lastChanges="17-05-2010" x="1" />
</items>
</root>
I had to make a couple of changes to get the sample code to compile and sort as desired:
The main thing was to provide an IFormatProvider so that the system could correctly parse the dates.
Output:
If you separate the concern of filtering and ordering the data into a testable class you should be able verify that ordering works correctly and focus your problem search elsewhere.
usage:
The next thing to look at would be, as @david-b suggested, whether the Repeater is reordering the inputs because the stringified XmlNode values would definitely come out in a different order if sorted as strings.