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

140
Views
How can i have third condition in my sorting?

I have the following array

let arr = [
    {
        auctionProductName: "m",
        pricePerUnitPerHour: 1,
        quanitity:20
    },
    {
        auctionProductName: "m",
        pricePerUnitPerHour: 22,
        quanitity:20
    },
    {
        auctionProductName: "a",
        pricePerUnitPerHour: 5555,
        quanitity:20
    },
    {
        auctionProductName: "a",
        pricePerUnitPerHour: 22,
        quanitity:20
    },
    {
        id:1,
        auctionProductName: "a",
        pricePerUnitPerHour: 22,
        quanitity:20
    },
   
    {
        auctionProductName: "m",
        pricePerUnitPerHour: 2222,
        quanitity:20
    },

    {
        id:2,
        auctionProductName: "a",
        pricePerUnitPerHour: 22,
        quanitity:2
    },
]

so this array need to be sorted firstly ASCENDING on auctionProductName, if there are same objects after this sorting by auctionProductName then they need to be sorted by pricePerUnitPerHour.

For that i have the following code which works as expected

function defaultTableSort() {
    arr = arr.sort(
        function (a, b) {
          if (a.auctionProductName === b.auctionProductName) {
            return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
          }
          return a.auctionProductName > b.auctionProductName ? 1 : -1;
    });
}

after the sorting i get

[
    {
        "auctionProductName": "a",
        "pricePerUnitPerHour": 22,
        "quanitity": 20
    },
    {
        "id": 1,
        "auctionProductName": "a",
        "pricePerUnitPerHour": 22,
        "quanitity": 20
    },
    {
        "id": 2,
        "auctionProductName": "a",
        "pricePerUnitPerHour": 22,
        "quanitity": 2
    },
    {
        "auctionProductName": "a",
        "pricePerUnitPerHour": 5555,
        "quanitity": 20
    },
    {
        "auctionProductName": "m",
        "pricePerUnitPerHour": 1,
        "quanitity": 20
    },
    {
        "auctionProductName": "m",
        "pricePerUnitPerHour": 22,
        "quanitity": 20
    },
    {
        "auctionProductName": "m",
        "pricePerUnitPerHour": 2222,
        "quanitity": 20
    }
]

i can't find a way to modify my defaultTableSort function - so the third condition will be by quantity -

if the auctionProductName and pricePerUnitPerHour are same, than we should sort by quantity.

That means that the third object ( AFTER THE SORTING ) with id of 2 needs to be before the second object with id of 1.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could chain the wanted sorting.

For getting a descending sorting, you could exchange a and b.

const
    array = [{ auctionProductName: "m", pricePerUnitPerHour: 1, quanitity: 20 }, { auctionProductName: "m", pricePerUnitPerHour: 22, quanitity: 20 }, { auctionProductName: "a", pricePerUnitPerHour: 5555, quanitity: 20 }, { auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 20 }, { id: 1, auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 20 }, { auctionProductName: "m", pricePerUnitPerHour: 2222, quanitity: 20 }, { id: 2, auctionProductName: "a", pricePerUnitPerHour: 22, quanitity: 2 }];

array.sort((a, b) =>
    a.auctionProductName.localeCompare(b.auctionProductName) ||
    a.pricePerUnitPerHour - b.pricePerUnitPerHour ||
    a.quanitity - b.quanitity
);

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

about 4 years ago · Juan Pablo Isaza Report

0

Just add another if clause

function (a, b) {
      if (a.auctionProductName === b.auctionProductName) {
        if(a.pricePerUnitPerHour === a.pricePerUnitPerHour)
             return a.quantity - b.quantity;
        return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
      }
      return a.auctionProductName > b.auctionProductName ? 1 : -1;
}
about 4 years ago · Juan Pablo Isaza Report

0

You can other conditional structure for verify if:

a.pricePerUnitPerHour === b.pricePerUnitPerHour

with a final code:

function defaultTableSort() {
    arr = arr.sort(
        function (a, b) {
            
          if (a.auctionProductName === b.auctionProductName) {
            // auctionProductName is equal between a and b
            
            if (a.pricePerUnitPerHour === b.pricePerUnitPerHour) {
                // pricePerUnitPerHour is equal between a and b
                
                // sort based on: quantity
                return a.quantity - b.quantity
            }
            
            // sort based on: pricePerUnitPerHour
            return a.pricePerUnitPerHour - b.pricePerUnitPerHour;
          }
          
          // sort based on: auctionProductName
          return a.auctionProductName > b.auctionProductName ? 1 : -1;
    });
}
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!