-1

Here's my code:

function getDetailsForShipmentId() {
   //Fill code here
   var strTable="<table id='myTable' border='2'><tr><th>Shipment Number</th><th>Port</th><th>Date</th><th>Time</th><th>Status</th></tr>";
   var array=[];
   array=populateShipmentDetails();
   array.sort(sortFunction);
   var i, found, obj;
   var id=document.getElementById("shipmentId").value;

    for (i = 0; i < array.length; i++) {
        obj = array[i];

        if (obj.shipmentId == id) {

            strTable+="<tr><td>"+obj.shipmentId+"</td><td>"+obj.port+"</td><td>"+obj.date+"</td><td>"+obj.time+"</td><td>"+obj.status+"</td></tr>";


        }

    }
    strTable+="</table>";
    document.getElementById("result").innerHTML=strTable;
}

function sortFunction(a,b){  
    var dateA = new Date(a.date).getTime();
    var dateB = new Date(b.date).getTime();
    return dateA > dateB ? 1 : -1;  
};

I am able to get the result searching through the javascript array but the sorting is not happening. Could somebody help?

Here's my output:

screenshot of HTML table

melpomene
  • 79,257
  • 6
  • 70
  • 127
sougata das
  • 357
  • 2
  • 3
  • 13

2 Answers2

0

You can use this function to sort the array by the key

   /**
     * Sort object properties (only own properties will be sorted).
     * @param {object} obj object to sort properties
     * @param {string|int} sortedBy 1 - sort object properties by specific value.
     * @param {bool} isNumericSort true - sort object properties as numeric value, false - sort as string value.
     * @param {bool} reverse false - reverse sorting.
     * @returns {Array} array of items in [[key,value],[key,value],...] format.
     */
    function sortProperties(obj, sortedBy, isNumericSort, reverse) {
        sortedBy = sortedBy || 1; // by default first key
        isNumericSort = isNumericSort || false; // by default text sort
        reverse = reverse || false; // by default no reverse

        var reversed = (reverse) ? -1 : 1;

        var sortable = [];
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                sortable.push([key, obj[key]]);
            }
        }
        if (isNumericSort)
            sortable.sort(function (a, b) {
                return reversed * (a[1][sortedBy] - b[1][sortedBy]);
            });
        else
            sortable.sort(function (a, b) {
                var x = a[1][sortedBy].toLowerCase(),
                    y = b[1][sortedBy].toLowerCase();
                return x < y ? reversed * -1 : x > y ? reversed : 0;
            });
        return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
    }

using the function would be as below

sortProperties(array, 'date', true, false);
Eby Jacob
  • 1,292
  • 1
  • 7
  • 26
0

You need to convert your date from DD/MM/YYYY to YYYY/MM/DD format. Then, using localeCompare you can sort the dates.

const dates = ['01/07/2017','29/05/2017', '29/06/2017', '01/07/2017', '30/06/2017', '30/06/2017'];
const conversion = (date) => date.split('/').reverse().join();
dates.sort((a,b) => {
  const dateA = conversion(a),
        dateB = conversion(b);
  return dateA.localeCompare(dateB);
});
console.log(dates);
Hassan Imam
  • 16,414
  • 3
  • 29
  • 41