javascriptappceleratorappcelerator-titanium

How to sort a set of data by date in appcelerator


Does anybody know how to sort out a set of data in appcelerator? I tried out with some sample code and it worked fine. But while implementing in my scenario it's not working. In my case I added data into the DB and sorted it by calling data from DB. Date format is like this "yyyy-mm-dd hh:mm:ss"

Here is my code:

var DummyData = [];
var isDatess=true;

var sampleData= Alloy.createCollection('samplePage');
sampleData .fetch({query:"SELECT * FROM dummyValfromDB"});
if(sampleData.length > 0){
        isDatess=true;
        for (var i = 0; i < sampleData.length;i++) {
        var did = sampleData.at(i).get('did');
        var dummyName = sampleData.at(i).get('dummyName');
        var dummydate= sampleData.at(i).get('dummydate');
        var dummyuid= sampleData.at(i).get('dummyuid');

        sampleData.push({
             "label_dummy_name": { text: dummyName},
             "label_dummydate":{text: dummydate},
             "label_dummyuid":{text: dummyuid},      
        });
    }
        DummyData.sort(sorting);
}

  function sorting(a,b){  
    var dateA = new Date(a.label_dummydate).getTime();
    var dateB = new Date(b.label_dummydate).getTime();
    var result="";
    if(isDatess)
        result=dateA > dateB ? 1 : -1;  
    else
        result=dateA < dateB ? 1 : -1;
    return result;  

  }; 


$.dummyList.sections[0].setItems(DummyData);

Solution

  • I got the answer. NaN shows while executing and it resolved by adding text. Find the answer i got below.

      function sorting(a,b) {  
                var valA = a.label_dummydate.text;
                var valB = b.label_dummydate.text;
    
                var dateA = new Date(val_a).getTime();
                var dateB = new Date(val_b).getTime();
                var result="";
    
                if(isDatess)
                    result=dateA > dateB ? 1 : -1;  
                else
                    result=dateA < dateB ? 1 : -1;  
                return result;  
          }; 
    

    Thanks for all your help. I think it may help somebody....Cheers !!!!