0

I am using this code for comparing string values in a method, which work perfectly. But for some value it gives wrong value, for example below values:

Code:

$string1 = "65";
$string2 = "5-fold";
$result = strcasecmp($string1, $string2);

switch ($result) {
    case -1: print "65 comes before 5-fold"; break;
    case 0: print "65 and 5-fold are the same"; break;
    case 1: print "65 comes after 5-fold"; break;
}

Output:

65 comes after 5-fold

I use this code for sorted array list, which sort them like ( 65 comes before 5-fold ). may be this output of because the " - " or something else i don't know. Do you have any idea about this.....

Below code Sort the multi-dimensional array:

       foreach($index_terms as $c=>$key) {
            $sort_id[] = $key['id'];
            $sort_term[] = $key['term'];
            $sort_freq[] = $key['freq'];
        }

        array_multisort($sort_term, SORT_ASC, $index_terms);
4
  • 2
    6 comes after 5, no? so 65 should come after 5-fold. Commented Apr 23, 2012 at 6:48
  • yes you are right, but what i mean is to consider "5-fold" as a whole string....like what php function array_multisort() sort them..... Commented Apr 23, 2012 at 6:56
  • What is in $sort_term and $index_terms? Commented Apr 23, 2012 at 8:33
  • $index_terms is actually the big index terms array....and the sor_term is sorted terms array created for the sorting of big index terms array.... Commented Apr 23, 2012 at 10:25

3 Answers 3

1

You're comparing 65 to 5-fold. It returns -1 if 65 is less than 5-fold and 1 if 65 is greater than 5-fold.

65 is greater than 5-fold... I don't see the problem?

What is 5-fold supposed to be that you want 65 to come before it?

Sign up to request clarification or add additional context in comments.

4 Comments

yes: because i sorted the list already on "array_multisort()" which place 65 before 5-fold...
Show us the code that puts 65 before 5-fold, because I don't think we're understanding. array_multisort() can sort based off the ordering of another array and may not ever even be comparing 65 to 5-fold. I think the issue may be a misunderstanding of array_multisort()?
I have updated my question, for the array_multisort() method.
Have you tried array_multisort($sort_term, SORT_ASC, $index_terms, SORT_ASC);?
0

Code :

<?php
    $string1 = "65";
    $string2 = "5-fold";
    $result = strcasecmp($string1, $string2);

    echo $result;
?>

Output :

1


Hint : There's nothing wrong with your output. 1 means the second operand is greater, -1 means the first operand is greater.

1 Comment

I know it return 1, but i sorted the list on array_multisort() which place the 65 before 5-fold in the list...now i want a comparison which do work like array_multisort().....
0

try to use Intval

strcasecmp compare the binary value of your string

$string1 = intVal("65"); 
$string2 = intVal("5-fold");  
$result = strcasecmp($string1, $string2);

switch ($result) {
    case -1: print "65 comes before 5-fold"; break;
    case 0: print "65 and 5-fold are the same"; break;
    case 1: print "65 comes after 5-fold"; break; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.