Elite user
Elite user

Reputation: 101

Object Array Order by two Values in JS

i have the following object array

var parameter = "B001";
var my_array = [
                 {
                  "Shippment_out" : "2020-05-10",
                  "Batch" : "B001",
                  "Truck_No" : "ZB001"
                 },
                 {
                  "Shippment_out" : "2020-05-10",
                  "Batch" : "B002",
                  "Truck_No" : "ZB001"
                 },
                 {
                  "Shippment_out" : "2020-05-11",
                  "Batch" : "B001",
                  "Truck_No" : "ZB002"
                 },
                 {
                  "Shippment_out" : "2020-05-11",
                  "Batch" : "B002",
                  "Truck_No" : "ZB002"
                 },
               ]

i planning sort this by the parameter, if i pass batch no as B001 then the array should be sorted by B001 first then B002 together with earlier date first. in this case "2020-05-10".

likewise if i pass "B002" then "B002" then "B001". again earlier date first.

i tried my_array.sort(function (a, b) {}) but not able to implement the logic.

Upvotes: 0

Views: 57

Answers (3)

Brendon Hudson
Brendon Hudson

Reputation: 36

You need to join the important fields to sort them. Just concatenate batch string with date string like B00120200511 before the comparations inside your sort function.

Edit: To solve your prioritization batch problem, concatenate them with '0' string.

Code with your array and a function to sort it with optional batch priorization

var my_array = [
  {
    "Shippment_out": "2020-05-10",
    "Batch": "B001",
    "Truck_No": "ZB001"
  },
  {
    "Shippment_out": "2020-05-10",
    "Batch": "B002",
    "Truck_No": "ZB001"
  },
  {
    "Shippment_out": "2020-05-11",
    "Batch": "B001",
    "Truck_No": "ZB002"
  },
  {
    "Shippment_out": "2020-05-11",
    "Batch": "B002",
    "Truck_No": "ZB002"
  },
];

/**
 * function to sort any array with .Batch and .Shippment_out fields and optionally prioritization of a Batch option
 * @param {Array} arr An array with fields arr.Batch and arr.Shippment_out
 * @param {String} priority Optional field used if you want to prioritize a Batch in the sort
 */
const sortMyArray = (arr, priority = '') => arr.sort((a, b) => {
  // First: concat strings batch with shippment date
  const a_id = (a.Batch === priority ? '0' : a.Batch) + a.Shippment_out;
  const b_id = (b.Batch === priority ? '0' : b.Batch) + b.Shippment_out;

  // Second: compares the new Id with important data to sort
  return a_id >= b_id ? 1 : -1;
});

// to use, just call sortMyArray. You can call with batch priorization
const result1 = sortMyArray(my_array, 'B002');
console.log(result1);
// or without batch priorization
const result2 = sortMyArray(my_array);
console.log(result2);

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

In the sort method, check a.Batch and b.Batch values.
if both values are same as required batch then sort it based on shipment.
if one of value is same as batch, then return accordingly.

var my_array = [
  {
    Shippment_out: "2020-05-10",
    Batch: "B001",
    Truck_No: "ZB001"
  },
  {
    Shippment_out: "2020-05-10",
    Batch: "B002",
    Truck_No: "ZB001"
  },
  {
    Shippment_out: "2020-05-11",
    Batch: "B001",
    Truck_No: "ZB002"
  },
  {
    Shippment_out: "2020-05-11",
    Batch: "B002",
    Truck_No: "ZB002"
  }
];

const sortArray = (arr, batch) => {
  return arr.sort((a, b) => {
    if (a.Batch === batch && b.Batch === batch) {
      return new Date(a.Shippment_out) - new Date(b.Shippment_out);
    } else if (a.Batch === batch) {
      return -1;
    } else if (b.Batch === batch) {
      return 1;
    } else {
      return 0;
    }
  });
};

console.log('B001', sortArray(my_array, "B001"));
console.log('B002', sortArray(my_array, "B002"));

Upvotes: 1

Jayesh Tanna
Jayesh Tanna

Reputation: 418

You can design your sort function like this:

items.sort(function(a, b) {
     return a.Batch - b.Batch  ||  a.Shippment_out - b.Shippment_out;
});

Here is the logic: if a.Batch - b.Batch expression evaluates to 0 (so these properties are equal), it will proceed with evaluating || expression - and return the result of a.Shippment_out - b.Shippment_out.

Upvotes: 0

Related Questions