Scooby08 Posted August 16, 2010 Share Posted August 16, 2010 Sample array: array('one','two','three','_zero'); How could I sort this array so that _zero moves to the front.. Any items that begin with an underscore should be moved to the front of the array to get something like so: _zero, one, two, three Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/ Share on other sites More sharing options...
Psycho Posted August 16, 2010 Share Posted August 16, 2010 You are going to have to write a custom sorter since there is no "logical" manner in which to sort the values. Those are string values, so PHP doesn't know how to interpret those strings back to their numeric values (which is apparently how you want them sorted). Without knowing all the possible values you intend to use I can't provide a full solution. BUt, I will suggest you will want to use usort() or uasort() along with a custom function. If you provide more details on all the possible values and how they should be sorted I might be able to provide some code to get you started. Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099916 Share on other sites More sharing options...
Scooby08 Posted August 16, 2010 Author Share Posted August 16, 2010 Well the possible values could be any string without an underscore in front.. There won't always be one with an underscore, but if there is I'd like to move it to the front some how.. The one with the underscore will always start with the underscore.. It will not be in the middle or at the end.. Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099921 Share on other sites More sharing options...
Psycho Posted August 16, 2010 Share Posted August 16, 2010 OK, so if you have two values with underscores, what is the logic to determine which one comes first? Here is a small function that will move values with underscores to the front, but there is no logic for putting them in any order at the front. Plus, without logic to sort the underscore values AND the other values, the current order will likely get messed up. This will put the underscore values at the beginning but will mess up the order of the other values: $testArray = array('one', '_two', 'two', '_three', 'three','_zero'); function sortUnderscoreToFront($a, $b) { if(substr($a, 0, 1)=='_') { return -1; } if(substr($b, 0, 1)=='_') { return 1; } return 1; } usort($testArray, 'sortUnderscoreToFront'); print_r($testArray); Output: Array ( [0] => _zero [1] => _two [2] => _three [3] => one [4] => three [5] => two ) If you need to maintain the order of the non-underscore values OR need the underscore values in a specific order it will require a more complex solution. Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099928 Share on other sites More sharing options...
Scooby08 Posted August 16, 2010 Author Share Posted August 16, 2010 Yep.. This is working really well, but like you say, I would really like to order the non underscore values.. I'm gonna keep at it, but if you have any ideas I'd love to hear them!! Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099941 Share on other sites More sharing options...
Scooby08 Posted August 16, 2010 Author Share Posted August 16, 2010 Well I did finally figure it out.. This seems to do the trick for me: <?php function sortUnderscoreToFront($a, $b) { if (substr($a, 0, 1) == '_') { return -1; } if (substr($b, 0, 1) == '_') { return 1; } return strcmp(strval($a), strval($b)); } usort($testArray, 'sortUnderscoreToFront'); ?> It alphabetically sorts all the non underscore values alphabetically while still moving all the underscore values to the front.. It would still need some logic to alphabetically sort the underscore values if wanted.. Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099949 Share on other sites More sharing options...
Psycho Posted August 16, 2010 Share Posted August 16, 2010 Um, why didn't you SAY you wanted the non-underscore values sorted alphabetically. I stated that I assumed you wanted them sorted how they were originally so they would be in order to their numerical equivalence. If you want both the underscore and non-underscore values sorted alphabetically - that is easy. function sortUnderscoreToFront($a, $b) { if(substr($a, 0, 1)=='_' Xor substr($b, 0, 1)=='_') { //One value starts with an underscore and the other does not return (substr($a, 0, 1)=='_') ? -1 : 1; } //Both start with an underscore or both do not return strcmp(strval($a), strval($b)); } Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099958 Share on other sites More sharing options...
Scooby08 Posted August 16, 2010 Author Share Posted August 16, 2010 I did end up saying that a couple posts ago, but I didn't initially say that because I didn't know what the solution was going to do to the array.. I started with a sort($array) and then ran this function to compare.. The sort ordered them and the function scrambled them again.. Anyways.. Now it works exactly perfect and we even got a few different ways to do the function.. Thanks a bunch mjdamato!! Quote Link to comment https://forums.phpfreaks.com/topic/210880-order-array-items-moving-underscores-to-front/#findComment-1099961 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.