0

I want to sort the nested arrays by salary beginning with the person who receives the highest salary and ending with the person who receives the lowest one. I dont want to take into consideration the currency... I dont know how to do it... Thanks in advance

Array
(
[0]=>Array
(
[firstName]=>John
[lastName]=>Doe
[age]=>35
[salary]=>Array
(
[gbp]=>"180"
[eur]=>“”
[usd]=>""
)
)
[1]=>Array
(
[firstName]=>Maria
[lastName]=>Anders
[age]=>26
[salary]=>Array
(
[gbp]=>""
[eur]=>"100"
[usd]=>""
)
)
[2]=>Array
(
[firstName]=>Thomas
[lastName]=>Hardy
[age]=>31
[salary]=>Array
(
[gbp]=>""
[eur]=>""
[usd]=>"224"
)
)
)
2
  • 3
    Please, try something and come back after you try. Write what you tried also here so that we can help you. This is not a "do my homework" website. Commented Nov 22, 2017 at 6:30
  • 2
    At least provide data in a formatted manner. Commented Nov 22, 2017 at 6:33

3 Answers 3

1

It looks like you posted php.

Anyways, take a look at the sort function:

var numbers = [{
  age: 10,
  salary: {
    gbp: 10,
    eur: 0,
    usd: 0
  }
}, {
  age: 2,
  salary: {
    gbp: 0,
    eur: 345,
    usd: 0
  }
}, {
  age: 100,
  salary: {
    gbp: 0,
    eur: 0,
    usd: 23
  }
}, {
  age: 50,
  salary: {
    gbp: 0,
    eur: 432,
    usd: 0
  }
}];

numbers.sort(function(a, b) {
  // Get the sum of salaries in the a object
  let aAmt = Object.values(a.salary).reduce((s, v) => s + v, 0)
  // Get the sum of salaries in the b object
  let bAmt = Object.values(b.salary).reduce((s, v) => s + v, 0)
  // Calculate the difference
  return bAmt - aAmt
})

console.log(numbers);

Sign up to request clarification or add additional context in comments.

Comments

1

I don't think what you posted is JS, but if you have a similar type of array in JS you could try to use the following

Assuming the array looks something like this

var x = [['John', 'Doe', 35, [180,0,0]],['Maria','Anders',26,[0,100,0]],['Thomas','Hardy',31,[0,0,224]]]

You could then use this

function sorty(a,b){
    if(Math.max.apply(null, a[3]) > Math.max.apply(null, b[3])) return -1;
    if(Math.max.apply(null, a[3]) < Math.max.apply(null, b[3])) return 1;
    return 0
}

Then running this:

x.sort(sorty)

Will give you this:

    0: (4) ["Thomas", "Hardy", 31, Array(3)]
    1: (4) ["John", "Doe", 35, Array(3)]
    2: (4) ["Maria", "Anders", 26, Array(3)]

Sort of a hacky solution I guess.

Comments

0

While you have only one relevant value in the salary object, you could take logical OR || for getting a value out of it for sorting. If no value is truthy, a zero is taken.

In the callback of Array#sort take the delta of the two values, a - b sorts smaller a before b. If you like to sort descending, just reverse the operands to b - a.

var array = [{ firstName: "John", lastName: "Doe", age: 35, salary: { gbp: "180", eur: "", usd: "" } }, { firstName: "Maria", lastName: "Anders", age: 26, salary: { gbp: "", eur: "100", usd: "" } }, { firstName: "Thomas", lastName: "Hardy", age: 31, salary: { gbp: "", eur: "", usd: "224" } }];

array.sort(function (a, b) {
    var aa = a.salary.gbp || a.salary.eur || a.salary.usd || 0,
        bb = b.salary.gbp || b.salary.eur || b.salary.usd || 0;

    return bb - aa;
});

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.