Sorting Results Alphabetically

Permalink Browser Info Environment
I was wondering if it was possible to sort locations alphabetically rather than by distance?

Type: Discussion
Status: New
mobasweb
View Replies:
ryan replied on at Permalink Reply
ryan
You would have to modify the code...

I'm assuming that you'd want the results first filtered by distance from the search address, then sorted alphabetically.

You'd want to override the blocks controller by copying:
/package/dealer_locator/blocks/dealer_search/controller.php
to:
/blocks/dealer_search/controller.php

Then around line 124 you'd comment out the sort by distance and add sort by name.
//$distanceList->sortBy('distance','ASC'); // existing code
$distanceList->sortBy('name','ASC');
mobasweb replied on at Permalink Reply
mobasweb
Worked perfectly. Thank you.
FatTony1952 replied on at Permalink Reply
FatTony1952
Does this work with Type/Category as well? I've tried it with the Type attribute, but it still always sorts by distance.

I've tried sorting both ascending and descending and have had no luck.

I changed:
$distanceList->sortBy('distance','ASC');


to:

$distanceList->sortBy('type','DESC');


Initially I wanted to sort by distance first and then type/category, so I changed the line to the code below and it didn't work either.

$distanceList->sortByMultiple('distance ASC','type ASC');
mkly replied on at Permalink Reply
mkly
Hello,
I believe that as it stands it sorts by distance no matter what(the distance calculation has actually been baked into the query). If you want to sort by anything else I would just create a new DatabaseItemList

$dil = new DatabaseItemList;
$dil->addToQuery('SELECT * FROM DealerLocatorLocations');
$dil->sortBy('type', 'DESC');


Best Wishes,
Mike
FatTony1952 replied on at Permalink Reply
FatTony1952
If that's the case, I wonder why this line is needed?

$distanceList->sortBy('distance','ASC');
mkly replied on at Permalink Reply
mkly
Ah, yes. My SQL is a bit rusty. I'm surprised that isn't working for you. I'm going to set up a test and see if I can sort that out.

Best Wishes,
Mike
FatTony1952 replied on at Permalink Reply
FatTony1952
I really only have two types of dealers to list and I tried an if/else statement, but my php ain't the greatest either, so it didn't work.

That's when I scoured the C5 documentation and found the sortByMultiple command.

Thanks for looking into it!
mkly replied on at Permalink Reply
mkly
Ok, I can probably just give you some SQL to do this. Let me know the exact results you want and I'll write something for it real quick.

Best Wishes,
Mike
FatTony1952 replied on at Permalink Reply
FatTony1952
I'm using the Type/Category option to indicate a "Gold Dealer". These "Gold Dealers" will be listed first in the sidebar results, then by distance.

I'm currently working on showing a gold icon beside of the Gold Dealers information in the sidebar as well.

Thanks!
mkly replied on at Permalink Reply
mkly
Alright, finally broke down and read through the query. The reason you can sort from type is the query isn't selecting it.

Around Line #123 of /dealer_location/models/dealer_locator.php
$q = "SELECT address, address2, postal, name, city, state, lat, lng, phone, country, extra, url,

You'll notice that there isn't type. Let's add that.
$q = "SELECT type, address, address2, postal, name, city, state, lat, lng, phone, country, extra, url,

Then you should be able to do that sortByMultiple() call just fine. *Hopefully.

Best Wishes,
Mike
FatTony1952 replied on at Permalink Reply
FatTony1952
Still no dice. I've added 'type' to the query and it still only sorts by distance alone. I've tried all of the following ways:

$distanceList->sortByMultiple('distance asc','type desc');
$distanceList->sortByMultiple('distance,asc','type,desc');
$distanceList->sortByMultiple('distance','ASC','type,'DESC');
mkly replied on at Permalink Reply
mkly
Hi Tony,
I was way off. I didn't realize this was actually in the block.

/dealer_locator/blocks/dealer_search/tools/search.php
Line #5

$distanceList->sortBy('distance','ASC');

to
$distanceList->sortBy('name', 'DESC');


Sorry about that.

Best Wishes,
Mike

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.