Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
Sort function with undefined value

I have an array of object like below:

 var stores = [
      {
        "name": "store3",
        "ditance": 8
      },
      {
        "name": "Store5",
        "distance": 7,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isFutureOrderPossible": false
          }
        }
      },
      {
        "name": "Store1",
        "distance": 12,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isOpen": true
          }
        }
      },
      {
        "name": "store2",
        "distance": 13,
        "web": {
          "validateAttributes": {
            "isSelectedDispostionSupported": true,
            "isOpen": true
          }
        }
      }
    ]

Expected Result to be sort based on isopen and distance

[
  {
    "name": "Store1",
    "distance": 12,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isOpen": true
      }
    }
  },
  {
    "name": "store2",
    "distance": 13,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isOpen": true
      }
    }
  },
  {
    "name": "Store5",
    "distance": 7,
    "web": {
      "validateAttributes": {
        "isSelectedDispostionSupported": true,
        "isFutureOrderPossible": false
      }
    }
  },
  {
    "name": "store3",
    "ditance": 8
  }
]

Need to sort an array based on isOpen and distance .The challenge is some object have web property and some object don't have. Even if web available sometimes isOpen won't their. I have tried the below approch it's not working

const sorter = (a, b) => {
                if (a.web) {
                  if (a.web.validateAttributes) {
                    if (a.web.validateAttributes.isOpen) {
                      return 1;
                    } else if (b.web.validateAttributes.isOpen) {
                      return -1;
                    } else {
                      return 1;
                    };
                  } else {
                    return 1;
                  }
                 
                } else {
                  return 1;
                }
              };
  stores.sort(sorter);
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try this

const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]

   const sorter = (a,b)=> {
      if(!b.web) {
          return -1;
      }
      else if(!b.web.validateAttributes) {
        return -1;
      }
      else if(!b.web.validateAttributes.isOpen) {
        return -1;
      }
      return (a.distance - b.distance)
   }
   console.log(stores.sort(sorter)) 

about 4 years ago · Juan Pablo Isaza Report

0

Try this.

const stores = [{"name":"store3","ditance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}]

const sorted = stores.slice();

sorted.sort((a, b) => {
    if (!a.web || !b.web) {
        return -1;
    }
    if (!a.web.validateAttributes || !b.web.validateAttributes) {
        return -1;
    }
    const x = a.web.validateAttributes.isOpen || false;
    const y = b.web.validateAttributes.isOpen || false;

    return (x === y) ? a.distance - b.distance : -1;
})
console.log(sorted)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

I see 2 problems.

  1. typo at the first item "ditance": 8
  2. Your sorting function. Here is my logic. Firstly, check both are Opening or Closing, then compare distance. Else just simple compare the state open/close

var stores = [{"name":"store3","distance":8},{"name":"Store5","distance":7,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isFutureOrderPossible":false}}},{"name":"Store1","distance":12,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}},{"name":"store2","distance":13,"web":{"validateAttributes":{"isSelectedDispostionSupported":true,"isOpen":true}}}];
    
stores.sort((a, b) => {
  if (a.web?.validateAttributes?.isOpen && b.web?.validateAttributes?.isOpen || !a.web?.validateAttributes?.isOpen && !b.web?.validateAttributes?.isOpen) {
    return a.distance - b.distance;
  }
  if (a.web?.validateAttributes?.isOpen) {
    return -1;
  }
  return 1;
})

console.log(stores);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!