ShortSchoolBus Posted April 1, 2009 Share Posted April 1, 2009 Hi, I'm having some difficulty with this and was hoping someone can show me an efficient way to sort an array such as this array('1', '2-1', '2-2-1', '2-2', '2', '3-1', '3'); to array('1', '2', '2-1', '2-2', '2-2-1', '3', '3-1'); with no length restrictions. eg., 3-3-2-2-1 or 4-5-4-3-3-2-1 Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/ Share on other sites More sharing options...
JD* Posted April 1, 2009 Share Posted April 1, 2009 Try $array = array('1', '2-1', '2-2-1', '2-2', '2', '3-1', '3'); sort($array); print_r($array); I think that will work for you. Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799076 Share on other sites More sharing options...
ShortSchoolBus Posted April 2, 2009 Author Share Posted April 2, 2009 No, actually, regular sort will return the same array('1', '2-1', '2-2-1', '2-2', '2', '3-1', '3'); This is how a file system is sorted by default, but I need the result to be array('1', '2', '2-1', '2-2', '2-2-1', '3', '3-1');. I hope you understand what I mean. Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799082 Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 really, because when I ran his script, I got Array ( [0] => 1 [1] => 2 [2] => 2-1 [3] => 2-2 [4] => 2-2-1 [5] => 3 [6] => 3-1 ) Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799092 Share on other sites More sharing options...
ShortSchoolBus Posted April 2, 2009 Author Share Posted April 2, 2009 You're right. More specifically, this is what I was trying to sort array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php'); Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799116 Share on other sites More sharing options...
ShortSchoolBus Posted April 2, 2009 Author Share Posted April 2, 2009 I came up with a solution, but it's not very efficient $pages = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php'); $pos_array = array(); foreach ($pages as $page) { $page_name = substr($page, 0, strpos($page, ".php")); $parts = explode("_",$page_name); $position_string = $parts[1]; array_push($pos_array,$position_string); } sort($pos_array); $page_array = array(); foreach ($pos_array as $pos) { foreach ($pages as $page) { $page_name = substr($page, 0, strpos($page, ".php")); $parts = explode("_",$page_name); $position_string = $parts[1]; if($pos == $position_string){ array_push($page_array,$page); } } } $pages = $page_array; Output: [0] => page_1.php [1] => page_2.php [2] => page_2-1.php [3] => page_2-2.php [4] => page_2-2-1.php Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799124 Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 dunno if its necessarily faster or not, but you could also do this: $array = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php', 'page_3-1.php', 'page_3.php'); function format(&$file, $key, $t) { $file = ($t == 'strip')? preg_replace('~(page_|\.php)~','',$file) : 'page_' . $file . '.php'; } array_walk($array, 'format','strip'); sort($array); array_walk($array, 'format','build'); Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799147 Share on other sites More sharing options...
Salkcin Posted April 2, 2009 Share Posted April 2, 2009 and to tweak it a bit more... function DoSort($a, $b) { $strip = array('page_', '.php'); return (str_replace($strip, '', $a) < str_replace($strip, '', $b)) ? -1 : 1; } $array = array('page_1.php', 'page_2-1.php', 'page_2-2-1.php', 'page_2-2.php', 'page_2.php', 'page_3-1.php', 'page_3.php'); usort($array, 'DoSort'); Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799176 Share on other sites More sharing options...
ShortSchoolBus Posted April 2, 2009 Author Share Posted April 2, 2009 Yes, that's a bit tidier than mine. Thanks for taking the time to post your replies. Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799398 Share on other sites More sharing options...
ShortSchoolBus Posted April 2, 2009 Author Share Posted April 2, 2009 My intention is to use the filename to determine it's position/depth within a list. Example - <ul> <li>page_1.php</li> <li>page_2.php <ul> <li>page_2-1.php</li> <li>page_2-2.php</li> </ul> </li> <li>page_3.php <ul> <li>page_3-1.php</li> </ul> </li> </ul> If anyone has a suggestion for an efficient method for this, please post. Quote Link to comment https://forums.phpfreaks.com/topic/152152-array-sort-help/#findComment-799404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.