Home > Software engineering >  Reorder Object Data
Reorder Object Data

Time:07-30

I'm seeking out some advice to reorder this data efficiently. I want to order it by considering whether the outlet is a warehouse or not. Then, for the second order, if it is not a warehouse, I need to put the internal outlets on the second level. The Externals are put the last. Here is an example of the data:

{
  1: { outlet_id: "1",
       outlet_is_warehouse: 0,
       outlet_name: "1010 - City Square Mall",
       outlet_type: "External"
     },
  483: { outlet_id: "483",
         outlet_is_warehouse: 1,
         outlet_name: "Main Warehouse (07-28)",
         outlet_type: "Internal",
       },
  542: { outlet_id: "542",
         outlet_is_warehouse: 0,
         outlet_name: "Singapore Express 010",
         outlet_type: "Internal",
       },
  482: { outlet_id: "482",
         outlet_is_warehouse: 1,
         outlet_name: "Main Warehouse (07-29)",
         outlet_type: "Internal",
       },
}

Here is my sorting code:

let data = {
      1: { outlet_id: "1",
           outlet_is_warehouse: 0,
           outlet_name: "1010 - City Square Mall",
           outlet_type: "External"
         },
      483: { outlet_id: "483",
             outlet_is_warehouse: 1,
             outlet_name: "Main Warehouse (07-28)",
             outlet_type: "Internal",
           },
      542: { outlet_id: "542",
             outlet_is_warehouse: 0,
             outlet_name: "Singapore Express 010",
             outlet_type: "Internal",
           },
      482: { outlet_id: "482",
             outlet_is_warehouse: 1,
             outlet_name: "Main Warehouse (07-29)",
             outlet_type: "Internal",
           },
    };

function sortingWarehouseInternalExternal(product_outlet) {
          let temp = Object.values(product_outlet).sort((outlet) => {
            if (outlet.outlet_is_warehouse == 1) return -1;
            else if (outlet.outlet_type.toLowerCase() == "external") return 0;
            else return  1;
          });
          return temp;
    };

let temp = sortingWarehouseInternalExternal(data);

console.log(JSON.stringify(temp));

For the warehouse, it seems correct but putting the internal before the external seems not to work properly. Do you have some suggestions? The output in object or array of objects will be accepted.

CodePudding user response:

Please check this solution. It just regular sort with multiple citeria.

const data = {1: {outlet_id: "1",outlet_is_warehouse: 0,outlet_name: "1010 - City Square Mall",outlet_type: "External"},483: {outlet_id: "483",outlet_is_warehouse: 1,outlet_name: "Main Warehouse (07-28)",outlet_type: "Internal"},542: {outlet_id: "542",outlet_is_warehouse: 0,outlet_name: "Singapore Express 010",outlet_type: "Internal"},482: {outlet_id: "482",outlet_is_warehouse: 1,outlet_name: "Main Warehouse (07-29)",outlet_type: "Internal"}};

const values = Object.values(data);
const sorted = values.sort((a, b) => 
  (b.outlet_is_warehouse - a.outlet_is_warehouse) || 
  (b.outlet_type.localeCompare(a.outlet_type))
);

console.log(sorted);
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related