Am having some confusion as to the best strategy for resolving a peculiar issue with an A-Z index style of listing page.
In the controller:
class Directory_Controller
public function AlphabeticalIndex() {
return DataObject::get('Listing', ... , 'Name ASC')->GroupedBy('FirstLetter');
}
class Listing
public function getFirstLetter() {
return $this->Name[0];
}
This provides exactly the data structure I want in the template, but in order to have a sensible order of listings, without the 'T' section being much longer than all the others, I have to remove listings starting with 'The' from the first letter check:
public function getFirstLetter() {
return (substr($this->Name, 0, 4) == 'The ') ? $this->Name[4] : $this->Name[0];
}
Okay, great. This is working perfectly, but for one weird issue with the sort order of the listings.
Because the ASC sorting is done on the query itself, by the time the grouping operation happens, the order doesn’t change (and I think you can understand why I am less than enthusiastic about doing this operation in SQL, considering how much slower and more annoying the substring checks would be at the DB level). This seems to be fine if there are already listings under a letter index, but if not, then listings starting with 'The' get thrown to the end of the list.
eg:
E
- Equitycorp
- The E Company
G
- Goldcorp
- The G Company
But remove one of those entries, and it ends up out of order, like:
G
- Goldcorp
- The G Company
E
- The E Company
What is the best way to get around this? Can anyone point me to a kind of best practice guide for dealing with 'stop words' in human readable indexes/titles. Personally, I much prefer having the user see 'The E Company' rather than 'E Company, The', but I’d like to get a better idea of how others have approached this situation.
A-Z index pages are not an uncommon use case (at least for certain kinds of sites), but I'm not sure that too many people would have the whole 'The' issue, so maybe this is an outlier.
For now, I’ve patched DataObjectSet->groupBy(), to ksort() the results before giving the list back to the DataObjectSet->GroupedBy()