I have an XML structure and a date value. I need to sort and get the latest node from below xml. I am looking for descending sorting below xml based on StartDate value.
<PriceList>
<PriceInfo>
<Timestamp>2015-02-19T06:33:10.255</Timestamp>
<Value xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >3</Value>
<Unit>eur/kwh</Unit>
<StartDate>2015-02-16T00:00:00</StartDate>
<EndDate>2015-02-16T00:00:00</EndDate>
<BaseAmount xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />
<BaseAmountUnit />
</PriceInfo>
<PriceInfo>
<Timestamp>2015-02-11T06:43:10.255</Timestamp>
<Value xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />
<Unit>eur/kwh</Unit>
<StartDate>2015-02-11T00:00:00</StartDate>
<EndDate>2015-02-16T00:00:00</EndDate>
<BaseAmount xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >4</BaseAmount>
<BaseAmountUnit />
</PriceInfo>
<PriceInfo>
<Timestamp>2015-02-10T06:33:10.255</Timestamp>
<Value xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" >5</Value>
<Unit>eur/kwh</Unit>
<StartDate>2015-02-10T00:00:00</StartDate>
<EndDate>2015-02-16T00:00:00</EndDate>
<BaseAmount xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" />
<BaseAmountUnit />
</PriceInfo>
</PriceList>
I have written it like this,
var result = $(priceList).find("PriceInfo").sort(function (a, b) {
debugger;
var textA = $(a).find('StartDate').text();
var textB = $(b).find('StartDate').text();
if (textA < textB)
return 1;
if (textA > textB)
return -1;
return 0;
});