Sort part of an array by value of specific key

Neil Thompson
Neil Thompson used Ask the Experts™
on
Hi

I'm unsure how to sort by one of the key values in this array (it's part of a bigger one) but I want to loop through ERates->In and display "RateBreak" and "Rate" BUT sort by "Hours"

I'll obviously then do the same for ERates->Out

Thanks

  "ERates": {
    "In": [
      {
        "RateBreak": "½ Hour",
        "Rate": 100,
        "Hours": 0.5,
        
      },
      {
        "RateBreak": "1 Hour",
        "Rate": 200,
        "Hours": 1,
        
      },
      {
        "RateBreak": "Overnight",
        "Rate": 1000,
        "Hours": 10
      },
      {
        "RateBreak": "15 mins",
        "Rate": 70,
        "Hours": 0.25
        
      }
    ],
    "Out": [
      {
        "RateBreak": "½ Hour",
        "Rate": 150,
        "Hours": 0.5
        
      },
      {
        "RateBreak": "1 Hour",
        "Rate": 250,
        "Hours": 1
        
      },
      {
        "RateBreak": "Overnight",
        "Rate": 1200,
        "Hours": 10
      },
      {
        "RateBreak": "15 mins",
        "Rate": 100,
        "Hours": 0.25
        
      }
    ]
  }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
Use the .sort() function:

<!DOCTYPE html>
<html>
<body>
    <script>
        var obj={
            "ERates": {
                "In":  [{ "RateBreak": "½ Hour", "Rate": 100, "Hours": 0.5, },
                        { "RateBreak": "1 Hour", "Rate": 200, "Hours": 1, },
                        { "RateBreak": "Overnight", "Rate": 1000, "Hours": 10 },
                        { "RateBreak": "15 mins", "Rate": 70, "Hours": 0.25 }],
                "Out": [{ "RateBreak": "½ Hour", "Rate": 150, "Hours": 0.5 },
                        { "RateBreak": "1 Hour", "Rate": 250, "Hours": 1 },
                        { "RateBreak": "Overnight", "Rate": 1200, "Hours": 10 },
                        { "RateBreak": "15 mins", "Rate": 100, "Hours": 0.25 }]
            }
        };

        var sorted = obj.ERates.In.sort(function (a, b) { return a.Hours - b.Hours; });
        console.log(JSON.stringify(sorted, null, 1));
    </script>
</body>
</html>

Open in new window


Line 18 in modern JS

var sorted = obj.ERates.In.sort( (a, b) => a.Hours - b.Hours );

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
You use the usort() function

$in = usort($data->ERates->In, 'fnCompare');
function fnCompare($a, $b) {
	if ($a->Hours === $b->Hours) return 0;
	return $a->Hours < $b->Hours ? -1 : 1;
}

Open in new window

Notes: Assuming the above data is JSON it is not valid - you have commas after your data on lines 6 and 12.

I would have preferred to use this compare function
function fnCompare($a, $b) {
	return $a->Hours - $b->Hours;
}

Open in new window

But for some reason this sorts the array so that 1 < 0.25 < 10 - which does not make sense. I did not have time to debug why - maybe another expert can shed some insight.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
@Ste5an - I thought this was a PHP question?
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

ste5anSenior Developer

Commented:
oops..
Neil ThompsonSenior Systems Developer

Author

Commented:
Many thanks once again Julian you come to my assistance.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome Neil.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial