Page 1 of 1

Array Sorting Terms Containing Other Terms First

Posted: Sun May 31, 2009 1:52 pm
by cmagill
Hello,

I am attempting to write a little piece of php code and would appreciate some help.

I have an array of terms. Some are single words, others are phrases.

I need to sort the array so any terms containing other terms inside of them will be positioned before the terms they contain. The search should be case insensitive.

Just to make things interesting, some of the terms contain parenthesis, which means they stand for two terms (for example transfer(s) = transfer or transfers). These parenthesis containing terms should be checked against as the parenthesis term and the both alternatives, but moved as the single parenthesis term.

So this array,

array('Matter','Energy','Transfer(s)','Food web','Consumer','Producer','Predator','Prey','Energy transfer','Photosynthesis', 'Positive energy transfer(s)')

Would end up like this,

array('Positive energy transfer(s)','Energy transfer','Energy','Transfer(s)','Matter','Food web','Consumer','Producer','Predator','Prey','Photosynthesis')

or some other similar arrangement that satisfies the above conditions.

Thanks for any help.