Tuesday, 6 January 2015

Java Script : How to sort Javascript arraylist of objects


//How to sort Javascript arraylist of objects
The table like as shown in the figure.

 <script>
$(function(){
        // Intialized table id with datatable jquey
var table = $('#receiptsList').dataTable();

// ArrayList
var feeList = [];

$(table.fnGetNodes()).each(function(i){
var record = new Object();
record.receiptNo = $('button',table.fnGetData(i)[0]).text();
record.admissionNo = table.fnGetData(i)[1];
record.name = table.fnGetData(i)[2];
record.paidDate = table.fnGetData(i)[3];
record.paidAmount = table.fnGetData(i)[4];
record.paidType = table.fnGetData(i)[5];
record.userName = table.fnGetData(i)[6];

// Object added to arrayList
feeList.push(record);
});

// Sorting is called
         feeList.sort(SortByField);

});

function SortByField(a, b){
 var aName = a.receiptNo;
 var bName = b.receiptNo;
 return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

</script>

Finally arraylist sorted receiptNo wise.

No comments:

Post a Comment