Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda