1

I have an array, i need to sort the array based on one property if that exists.

const arr = [{
    "item_name": "Cake",
    "quantity": 1,
  },
  {
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,

  }
]

let output = arr.sort((a, b) => {
  if (a.out_of_stock_detail) {
    return 1
  } else {
    return -1
  }
})

console.log(output)

Expected output what I am looking out to get.

const result = [{
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,

  },
  {
    "item_name": "Cake",
    "quantity": 1,
  }
]
4
  • 2
    Property existence is checked with a.hasOwnProperty("out_of_stock_detail"). Checking a.out_of_stock_detail has different semantics. Why are you never checking b? Why are you never returning 0? Commented Dec 2, 2021 at 18:54
  • Didn't understood of checking with b and returning 0, can you help here not understanding what I am doing wrong here Commented Dec 2, 2021 at 19:01
  • Does this answer your question? How does Javascript's sort() work? Read all answers there, Commented Dec 2, 2021 at 19:06
  • @de The documentation describes how sort works. Commented Dec 2, 2021 at 19:06

4 Answers 4

2

Here's an option. Create a numeric value based on the existence of the property then return the comparison. I also added a second sort so highest quantities are first.

const arr = [{
    "item_name": "Cake",
    "quantity": 1,
  },
  {
    "item_name": "Choclate",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 3,
  },
  {
    "item_name": "Vanilla",
    "out_of_stock_detail": {
      "out_of_stock_quantity": 1
    },
    "quantity": 9,
  }
]

let output = arr.sort((a, b) => {
  let aa = a.hasOwnProperty('out_of_stock_detail') ? 1 : -1
  let bb = b.hasOwnProperty('out_of_stock_detail') ? 1 : -1
  return bb - aa;
}).sort((a, b) => +b.quantity - +a.quantity)

console.log(output)

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

1 Comment

This helps, also it supports one additional feature also thanks
1

Just a slightly other approach by taking a boolean and taking the delta of it.

BTW, Array#sort sorts in situ (mutating the array).

const
    array = [{ item_name: "Cake", quantity: 1 }, { item_name: "Choclate", out_of_stock_detail: { out_of_stock_quantity: 1 }, quantity: 3 }];

array.sort((a, b) => 
    ('out_of_stock_detail' in b) - ('out_of_stock_detail' in a)
);

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

Comments

0

really strange code, but like this?

let output = arr.sort((a,b) => {
   if(a.out_of_stock_detail && !b.out_of_stock_detail){
    return -1 
  }else{
    return 1
  }
})

wouldn't it be better to separate those?

let outOfStock = arr.filter((item) => item.out_of_stock_detail);
let inStock = arr.filter((item) => !item.out_of_stock_detail);

Comments

0

Didn't you just mix up the 1 and -1?

const arr =  [
        {
            "item_name": "Cake",
            "quantity": 1,
        },
        {
            "item_name": "Choclate",
            "out_of_stock_detail": {
                "out_of_stock_quantity": 1
            },
            "quantity": 3,
    
        }
    ]

let output = arr.sort((a,b) => {
   if(a.out_of_stock_detail){
    return -1 
  }else {
    return 1
  }
})

console.log(output)

Comments

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.