Jump to content

Recommended Posts

Hi, I've asked a related question before, but it was too specific, because it solved the problem I asked about, but didn't fix the the larger real problem. So this is the bigger problem.

 

I'm trying to sort an array of html links, (below as $feed)  in a "natural" way for a web index page. The array has about 1000 items so far.

 

Example source:

<?php
<a href="blackbird.php">Blackbird</a>
<a href="black_and_yellow_warbler.php">Black and Yellow Warbler</a>
<a href="müller.php">Müller</a>
<a href="munster.php">Munster</a>
<a href="2.php">2</a>
<a href="2008_show.php">2008 Show</a>
<a href="1998.php">1998</a>
?>

 

The rule for creating the links is that the visible link text is always mirrored in the <a href> link, but accents etc are always escaped, spaces replaced by underscores, capitals by lowercase. So I could potentially sort either by the link or the visible text - whichever was easier. Currently I'm sorting by the whole string, which is effectively by the html links.

 

Sorting should be as follows:

1. numbers should come before letters.

2. Caps should be ignored (treated as if lowercase)

3. spaces, hyphens, underscores, apostrophes should be ignored (treated as if they weren't there),

4. accented characters should operate like their non-accented couterparts (é should be treated like e, ü like u [i realise the latter is linguistically incorrect, but prefer my order])

 

Ideally also 

5. 9 should come before 10

6. 4H should come between 4 and 5

 

I'm currently using the code below, taken from php.net (which I'm not sure I really understand), which sorts out all of the problems regarding caps very nicely:

<?php
$array_lowercase = array_map('strtolower', $feed);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $feed);
?>

 

Before sorting I also do a find and replace on the few examples of accents and apostrophes. E.g. replacing John's with Johnsxxx, then sorting, then a reverse find and replace. I have to customise each one, which is labour intensive, but fortunately there are only a handful of cases. In fact the "xxx" has to be put further down the string in many cases, e.g. JohnsStarxxx, because otherwise the "xxx" would sort John's Starling after Johnston's Bluebird, which would be no good.

 

The main problem that remains though is spaces. Since spaces (or underscores) come before letters, Black Finch comes before Blackbird, whereas I want "Blackbird" to be between "Black and Yellow Warbler" and "Black Finch". There are many items with spaces (i.e. undescores in the link), far too many to write a find and replace for each one. So my question is, has anyone worked out a way to sort an array like this already…

 

As I write this I've just thought I could do a preg_replace on the spaces, replacing the character after the space with an uppercase first letter, and then to undo this effect after sorting, I could just look for camelCase type words (which there would not normally be any of) and put the space back in. Does this sound like a good idea?

 

Still, that doesn't solves the problem of the numbers. Currently it sorts like this (I think from memory) 1, 10, 1000, 102, 2, 2008 Show, 2B, 9, 99 etc. Any way to sort these in quasi-numerical value i.e. 1, 2, 2B, 9, 10, 102, 1000, 2008 Show?

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/242720-sorting-normally-with-array_multisort/
Share on other sites

Wait, I've confused myself on this one - I had forgooten - I'm actually sorting by the visible text on this (not the html) that's why I need to have rules for spaces and caps … Sorry about the confusion - but I could potentially sort by the links if I preferred.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.