View Full Version : Can PHP sorting functions handle this sort?
beetle
10-31-2002, 07:30 PM
I'm trying to sort the first dimension of a two dimensional array by a value in the 2nd dimension. I've tried my best to fully understand usort() and multisort() but can't seem to apply them to my need. Here's an example of the array data$results = [
['zip' => '75002', 'distance' => 0 ],
['zip' => '75013', 'distance' => 4.1],
['zip' => '75023', 'distance' => 6.9],
['zip' => '75025', 'distance' => 7 ],
['zip' => '75035', 'distance' => 9.5],
['zip' => '75044', 'distance' => 9.4],
['zip' => '75048', 'distance' => 8.4],
['zip' => '75069', 'distance' => 7.2],
['zip' => '75070', 'distance' => 8.7],
['zip' => '75074', 'distance' => 5.5]
];I want to sort each of the associative arrays within $results based on the value in $results[i]['distance']
Making sense? The final result would look like this$results = [
['zip' => '75002', 'distance' => 0 ],
['zip' => '75013', 'distance' => 4.1],
['zip' => '75074', 'distance' => 5.5],
['zip' => '75023', 'distance' => 6.9],
['zip' => '75025', 'distance' => 7 ],
['zip' => '75069', 'distance' => 7.2],
['zip' => '75048', 'distance' => 8.4],
['zip' => '75070', 'distance' => 8.7],
['zip' => '75044', 'distance' => 9.4],
['zip' => '75035', 'distance' => 9.5]
];
mordred
10-31-2002, 08:07 PM
That's not valid PHP syntax for defining arrays, or am I missing the obvious here? Perhaps you stayed for long in the JS forums... :D
Anyway, here's a quick hack for a custom sorting method:
$results = array();
$results[] = array('zip' => '75002', 'distance' => 0);
$results[] = array('zip' => '75013', 'distance' => 4.1);
$results[] = array('zip' => '75023', 'distance' => 6.9);
$results[] = array('zip' => '75025', 'distance' => 7 );
$results[] = array('zip' => '75035', 'distance' => 9.5);
$results[] = array('zip' => '75044', 'distance' => 9.4);
$results[] = array('zip' => '75048', 'distance' => 8.4);
$results[] = array('zip' => '75069', 'distance' => 7.2);
$results[] = array('zip' => '75070', 'distance' => 8.7);
$results[] = array('zip' => '75074', 'distance' => 5.5);
function distanceCompare($a, $b) {
if ($a['distance'] == $b['distance']) {
// distances are equal
return 0;
} else {
// unequal distances
return ($a['distance'] > $b['distance']) ? (1) : (-1);
}
}
usort($results, "distanceCompare");
var_dump($results);
Enjoy.
beetle
10-31-2002, 08:25 PM
Thanks mordred....
Ya, I know the above isn't valid PHP syntax, but I was pretty sure it would get my point across, which apparently it did...:D
Thanks, I'm gonna go try this out...
beetle
10-31-2002, 10:23 PM
Thanks mordred, it works well, but not with my class. There's gotta something missing that I need to do so this will function as part of my zipcode class....just visit the page to see the source and results...
mordred
10-31-2002, 10:28 PM
Which page?
beetle
10-31-2002, 10:36 PM
Oop, sorry, I thought I had linked to it
www.lanwizards.com/ziptest.php
mordred
10-31-2002, 11:57 PM
From what I see you don't reference the comparing function correctly. usort() normally expects the function to be in the global scope (though I'm not 100% sure on that), but you want to call an object's method. For this case, there's a special syntax provided which is explained in detail in the manual page (hint hint ;)). I tweaked the original function a little bit so that it should also work in an object context:
// Sort results by value
function sortResults($key) {
$this->sortField = $key;
usort($this->results, array($this, "distanceCompare"));
}
<rant>
This is one of those little nuisances you repeatedly encounter when applying object design principles to this language that seems to be aimed at Write-Once-Run-Away-Coders... even JavaScript is better and more consistent in this matter IMHO...
</rant>
beetle
11-01-2002, 05:19 PM
Thanks mordred
My brain was pretty well fried yesterday...I'll remember to better check the manual in the future...
Now I gotta find the easiest/quickest way to remove duplicate Zips from my data.... :D
P.S. I here ya on that rant. I'm pretty fresh to OO, but I understand what you mean. what do you think of the class I'm writing? Seems ok to me, but I have no formal training in this area....
mordred
11-01-2002, 11:39 PM
Originally posted by beetle
My brain was pretty well fried yesterday...I'll remember to better check the manual in the future...
Hey, I just wrote that because the manual is still the place that explains things better and more detailed than I could do in a single post, especially the user notes are invaluable advice. But I really now the feeling that you look over something all the time and if somebody tells you, you go "Doh..." :)
Now I gotta find the easiest/quickest way to remove duplicate Zips from my data.... :D
Shot from the hip: Use array_unique() or array_filter() with a callback function.
P.S. I here ya on that rant. I'm pretty fresh to OO, but I understand what you mean. what do you think of the class I'm writing? Seems ok to me, but I have no formal training in this area....
Nor do I, although I recently started studying computer science. But my programming courses are right now pretty braindead, imagine the teacher to spend 90 minutes to cover the switch statement... what a waste of time. :rolleyes:
Anyway, your page is currently unavailable to me (timeout?), but from what I recall from memory, your code looked very good and readable. If that weren't the case, I would not have been able to debug your code so fast.
For me the biggest step (and I'm currently just right at it) with OOP programming is the way you *think* about your code. Just creating an object with some properties and some methods is fine, but using the properties in a fashion similar to global variables is nothing more than standard procedural programming, provocatively spoken. The difficult issue is how you approach, design your application. How you divide your business logic into objects. How you structure the object hierarchy. And so on...
Well, that's what I found the hardest part to tackle and that's exactly what I'm currently still not very good at. The problem with PHP is IMHO that it lacks some feature I expect as a given for OOP, like asigning by reference or compund statements, or exceptions. If you are used to this language constructions by doing javascript, you will see that JS is far more matured than PHP is in this regard (IMHO). Hopefully the Zend Engine 2 will deal with this problem soon.
That's my blather for now, I'll check your code later and hope that the server is responding then. Happy Coding. :thumbsup:
beetle
11-02-2002, 12:33 AM
Thanks so much for the reply.
I'm gonna have to figure out what to do with the duplicate zip codes...I found out why they exist. Some zips span state borders, and there is a separate entry in the DB. For example, 10004 is in both New York and New Jersey. I've already figured the easiest way to remove them, I just don't know if I should now.
The server that code is posted on is turned off right now...and may be that way through the weekend. I know this because i work in the exact same office as the server, and we moved offices today. (When I say we, I'm referring to a 2-man company, me and my boss :D) Right now, my back hurts a bit...he's got some heavy stuff!
Anyhow, we just moved down the hall, into a bigger office (our teeny one wasn't made for a full rack) so he may go in tomorrow and finish setting things up. I'd be delighted to hear your thoughts after looking at the code again.
Have a good weekend :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.