1

Initial array

var array = [
  {
    "name": "Service-1",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 10
  },
  {
    "name": "Service-2",
    "status": "Cancelled",
    "startDate": "2020-04-03T00:00:00",
    "id": 9
  },
  {
    "name": "DG_3029_Export1",
    "status": "Approved",
    "startDate": "2020-03-01T00:00:00",
    "id": 11
  },
  {
    "name": "DG_3029_Export2",
    "status": "Approved",
    "startDate": "2020-04-02T00:00:00",
    "id": 12
  },
  {
    "name": "Service-10",
    "status": "Cancelled",
    "startDate": "2020-04-10T00:00:00",
    "id": 19
  }
];

Expected Result:

var array = [
      {
        "name": "DG_3029_Export2",
        "status": "Approved",
        "startDate": "2020-04-02T00:00:00",
        "id": 12
      },
      {
        "name": "DG_3029_Export1",
        "status": "Approved",
        "startDate": "2020-03-01T00:00:00",
        "id": 11
      },
      {
        "name": "Service-10",
        "status": "Cancelled",
        "startDate": "2020-04-10T00:00:00",
        "id": 19
      },
      {
        "name": "Service-2",
        "status": "Cancelled",
        "startDate": "2020-04-03T00:00:00",
        "id": 9
      },
      {
        "name": "Service-1",
        "status": "Cancelled",
        "startDate": "2020-04-02T00:00:00",
        "id": 10
      } 
    ];

Sort by date descending (this is what I was able to achieve till now)

   var array = [
      {
        "name": "Service-10",
        "status": "Cancelled",
        "startDate": "2020-04-10T00:00:00",
        "id": 19
      },
      {
        "name": "Service-2",
        "status": "Cancelled",
        "startDate": "2020-04-03T00:00:00",
        "id": 9
      },
      {
        "name": "Service-1",
        "status": "Cancelled",
        "startDate": "2020-04-02T00:00:00",
        "id": 10
      },
      {
        "name": "DG_3029_Export2",
        "status": "Approved",
        "startDate": "2020-04-02T00:00:00",
        "id": 12
      },
      {
        "name": "DG_3029_Export1",
        "status": "Approved",
        "startDate": "2020-03-01T00:00:00",
        "id": 11
      }

    ];

Fiddle: https://jsfiddle.net/fierce_trailblazer/gs9ek0oz/

My goal is to sort by date and then show all the objects with status="Approved" at the begining

Is it possible to sort by date and additional parameter using javascript sort?

var array = [{
    "name": "Service-1",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 10
  },
  {
    "name": "DG_3029_Export1",
    "status": "Approved",
    "startDate": "2020-03-01T00:00:00",
    "id": 11
  },
  {
    "name": "DG_3029_Export2",
    "status": "Approved",
    "startDate": "2020-04-02T00:00:00",
    "id": 12
  },
  {
    "name": "Service-10",
    "status": "Cancelled",
    "startDate": "2020-04-10T00:00:00",
    "id": 19
  },
  {
    "name": "DG_Export3",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 13
  },
  {
    "name": "Service-5",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 14
  },
  {
    "name": "Service-6",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 15
  },
  {
    "name": "Service-7",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 16
  },
  {
    "name": "DG_Export4",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 17
  },
  {
    "name": "DG_Export5",
    "status": "Cancelled",
    "startDate": "2020-04-02T00:00:00",
    "id": 18
  }
];

array.sort((a, b) =>
  new Date(b.startDate) - new Date(a.startDate)
)
for (let i = 0; i < array.length; i++) {
  console.log("Id:\t" + array[i].id + "\tStatus:\t" + array[i].status + "\tDate:\t" + array[i].startDate.substring(1, 10));
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

2

You need to sort by status first and then by date to get all approved items on top.

var array = [{ name: "Service-1", status: "Cancelled", startDate: "2020-04-02T00:00:00", id: 10 }, { name: "Service-2", status: "Cancelled", startDate: "2020-04-03T00:00:00", id: 9 }, { name: "DG_3029_Export1", status: "Approved", startDate: "2020-03-01T00:00:00", id: 11 }, { name: "DG_3029_Export2", status: "Approved", startDate: "2020-04-02T00:00:00", id: 12 }, { name: "Service-10", status: "Cancelled", startDate: "2020-04-10T00:00:00", id: 19 }];

array.sort((a, b) =>
    (b.status === "Approved") - (a.status === "Approved") ||
    b.startDate.localeCompare(a.startDate)
);

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392