Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda