0

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.

3 Answers 3

1

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"));

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

this way returns the array same way also parameter not considered, i've tried this way before. actually this is what i did before.
0

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);

2 Comments

Still not able to sort with parameter
@Eliteuser I have improved my solve with "sort with parameter" feature, look at it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.