0

I have an Array:

array(5) {
  [A] => array(2) {
    [price] => string(3) "800"
    [score] => string(1) "8"
  }
  [B] => array(2) {
    [price] => string(3) "800"
    [score] => string(2) "10"
  }
  [C] => array(2) {
    [price] => string(3) "800"
    [score] => string(2) "15"
  }
  [D] => array(2) {
    [price] => string(4) "1800"
    [score] => string(1) "4"
  }
  [E] => array(2) {
    [price] => string(4) "2800"
    [score] => string(1) "5"
  }
}

I have successfully sorted based on the price (ASC Order) by following: this

Problem is i need to apply second level sorting too based on the score if price is same (Highest score should come first).

The required array needed should be like:

array(
    5) {
      [C] => array(2) {
        [price] => string(3) "800"
        [score] => string(2) "15"
      }
      [B] => array(2) {
        [price] => string(3) "800"
        [score] => string(2) "10"
      }
      [A] => array(2) {
        [price] => string(3) "800"
        [score] => string(1) "8"
      }
      [D] => array(2) {
        [price] => string(4) "1800"
        [score] => string(1) "4"
      }
      [E] => array(2) {
        [price] => string(4) "2800"
        [score] => string(1) "5"
      }
    }

Not able to think which way i have to go. Can someone please help here.

AKG
  • 1
  • 1
  • Hi and welcome to StackOverflow. Please refer to https://stackoverflow.com/help/how-to-ask on how to ask a proper question and improve yours according the guidelines. To get started you should add the code you already tried and describe whats wrong with its output. – Fabian Schöner Sep 22 '17 at 07:33
  • From the first result you obtained, you can apply tour logic to group product with same price and perform the usort on each group based on score and finally merge the each group array to get your final result. – Ajith Sep 22 '17 at 08:22
  • @Ajith Way too overcomplicated. A single sort operation with two criteria does just fine. – deceze Sep 22 '17 at 08:28
  • @deceze How? Can you please show me the right direction? – AKG Sep 22 '17 at 08:40
  • Have you read the duplicate? It's spelled out in quite some detail there: https://stackoverflow.com/a/17364128/476 – deceze Sep 22 '17 at 08:49

0 Answers0