Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP sort function (alphabetically)

Options
  • 08-08-2007 8:22am
    #1
    Registered Users 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 Posts: 68,317 ✭✭✭✭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