Sort multiple Object arrays by value

Asked

Viewed 159 times

0

people have the following situation:

var input1 = {preco:valor1.toFixed(3), tipo:name1};
var input2 = {preco:valor2.toFixed(3), tipo:name2};
var input3 = {preco:valor3.toFixed(3), tipo:name3};
var input4 = {preco:valor4.toFixed(3), tipo:name4};

and I would like to display the values of these 4 arrays in ascending order of the 'price' as follows:

Lowest value: type: price Second lowest value: Tipo2: preco2 (...)

how can I sort the 4 arrays according to the value of one of the objects of each one of them?

EDIT to better illustrate:

var valor1 = 50;
var name1  = "nome1";
var valor2 = 20;
var name2  = "nome2";
var valor3 = 60;
var name3  = "nome3";
var valor4 = 10;
var name4  = "nome4";

var input1 = {preco:valor1.toFixed(3), tipo:name1};
var input2 = {preco:valor2.toFixed(3), tipo:name2};
var input3 = {preco:valor3.toFixed(3), tipo:name3};
var input4 = {preco:valor4.toFixed(3), tipo:name4};

and after ordering, in that case, should print

Lowest Value: nome4 - 10

Second Lowest Value: Nome2 - 20

Third Lowest Value: nome1 - 50

Quarto Menor Valor: nome3 - 60

  • 2

    I’m not seeing 4 arrays, I’m seeing 4 objects, what you want is that property preco of input1 receive the lowest value between valor1, valor2, valor3 and valor4?

  • no... there are 4 products... in input1 it is storing the value and the name of the product1 as well as in the other...

1 answer

1


to sort by value while maintaining the relationship with the name, you must assemble an array with your tuples, then perform a Sort...

but as we are ordering by value, we cannot accomplish a toFixed, as this will turn the value into a string...

var valor1 = 2002.46;
var valor2 = 1001.23;
var valor3 = 4004.92;
var valor4 = 3003.69;

var name1 = "name1";
var name2 = "name2";
var name3 = "name3";
var name4 = "name4";

var intl = new Intl.NumberFormat("pt-BR", { minimumFractionDigits: 3 });
var inputs = [
    { preco: valor1, tipo: name1 },
    { preco: valor2, tipo: name2 },
    { preco: valor3, tipo: name3 },
    { preco: valor4, tipo: name4 }
];

inputs = inputs.sort(function (inputA, inputB) {
  return inputA.preco > inputB.preco;
});
inputs.forEach(function (input) {
  input.preco = intl.format(input.preco);
});

var input1 = inputs[0];
var input2 = inputs[1];
var input3 = inputs[2];
var input4 = inputs[3];

console.log(input1, input2, input3, input4);

the output of the above code will be:

input1: {preco: "1.001,230", tipo: "name2"} 
input2: {preco: "2.002,460", tipo: "name1"} 
input3: {preco: "3.003,690", tipo: "name4"} 
input4: {preco: "4.004,920", tipo: "name3"}
  • but in this case the values will be ordered but the names will be in the same order... then it will no longer serve because if the order of the values changes that of the names must also change

  • 1

    @Heathz, I made the change.

  • exactly that... very good... thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.