Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP sort function (alphabetically)

  • 08-08-2007 08:22AM
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    I have a PHP function which is supposed to say if one string is alphabetically before another.


    function before($alpha,$beta)
    {
    	$arr = array(strtolower($alpha),strtolower($beta));
    	sort($arr);
    	if ($arr[0]==$alpha)
    	{
    		echo $alpha." before ".$beta."<br>";
    		return true;
    	} else {
    		echo $alpha." after ".$beta."<br>";
    		return false;
    	}
    }
    

    However if I insert $alpha as "Apple" and $beta as "Cherry". It echos 'Apple after Cherry' and returns false.

    How do I make this return the correct alphabetical order on these strings?


Comments

  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    sorry you code is slightly wrong, you need to loop the array before you can use $arr[0]

    the code should look like this
    function before($alpha,$beta)
    {
    	$arr = array(strtolower($alpha),strtolower($beta));
    	asort($arr);
    foreach ($arr as $value) {
    	if ($value=$alpha)
    	{
    		echo $alpha." before ".$beta."<br>";
    		return true;
    	} else {
    		echo $alpha." after ".$beta."<br>";
    		return false;
    	}
        
    }
    }
    


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Assuming that you're only taking two scalar string arguments as variables, it's much more efficient to just use strcmp to compare them
    function before($alpha,$beta)
    {
    	if (strcasecmp($alpha, $beta) < 0)
    	{
    		echo $alpha." before ".$beta."<br>";
    		return true;
    	} else {
    		echo $alpha." after ".$beta."<br>";
    		return false;
    	}
    }
    
    strcasecmp() is a case-insensitive comparision (removes the need for strtolower).
    strcmp() is case-sensitive.

    If you're going to use this function with user (or variable) input, then maybe check for special case. Off the top of my head, some of these are:
    1. The empty string
    2. All whitespace strings
    3. The case where $alpha == $beta.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    There are a couple of errors in that code, and both are in the initial comparison

    "$arr[0]==$alpha"

    1) The comparison is an "==", which checks if they are equal; it should be "<="
    2) The comparison is using the original value of $alpha, rather than the post-strtolower entry of $beta in the array. As it is, the code is checking if $arr[0]==$alpha (and given the code, that's essentially checking if $alpha is lowercase).

    The proper line (quick replacement of code) is

    $arr[0]<=$arr[1]

    As seamus said, though, you could avoid the array completely and use strcasecmp directly on $alpha and $beta, but try the above substitution first so that you're happy that the logic is working, before you go using a new function that you're not familiar with.


Advertisement