1

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;
                });
VJOY
  • 3,752
  • 12
  • 57
  • 90
  • Have you tried anything?? – Milind Anantwar Mar 16 '15 at 06:55
  • I have written this code, 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; }); – VJOY Mar 16 '15 at 07:09

2 Answers2

0
$.get('Pricelist.xml', function(PriceList){     
  var priceInfo= $(PriceList).find('PriceInfo');

 priceInfo.sort(function(a, b){
     return $(a).data('StartDate').localeCompare( $(b).data('StartDate') );
  });

  priceInfo.each(function(i,v){
    alert($(v).attr('StartDate'));
  });
}); 
Ravikiran butti
  • 1,668
  • 2
  • 11
  • 18
0

Below is Daniel Lidström answer applied in the above context, where boolean < > comparison is used for Date objects.

var result = $xml.find("PriceInfo").sort(function (a, b) {
    //debugger;
    var textA = $(a).find('StartDate').text();
    var dateA = new Date(textA);

    var textB = $(b).find('StartDate').text();
    var dateB = new Date(textB);

    if (dateA < dateB) return +1;
    if (dateA > dateB) return -1;

    return 0;
});

Fiddle

Community
  • 1
  • 1
jyrkim
  • 2,849
  • 1
  • 24
  • 33