1

I am sorting an array using values alphabetically, the array is as below:

Array ( 
[0] => test This 
[1] => This test 
[2] => again this test 
[3] => test again this 
[4] => this test again 
[5] => Dallas University Texas 
[6] => Texas Dallas University 
[7] => University Texas Dallas 
[8] => dallas University Texas 
[9] => Texas dallas University 
[10] => University Texas dallas 
[11] => Johnson Johnson 
[12] => Johnson Johnson
)  

the expected output when i sort should be as below:

again this test  
dallas University Texas  
Dallas University Texas  
Johnson Johnson  
test again this  
test This   
this test again   
Texas dallas University   
Texas Dallas University   
This test   
University Texas dallas   
University Texas Dallas  

my code is below:
the comparator

function compareValues($a,$b) {
  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
}
function transform($input){
  return usort($input,array($input,"compareValues"));
}
print_r($transform($input));

My current output cannot alphabetize the all values and is not case sensitive, it can only produce partially ordered array. It should be the comparator function that has some fault.

4 Answers 4

2

use php native function that is

sort(array); and here is a link for your help http://www.w3schools.com/php/php_arrays_sort.asp

or if you want sorting alphabetically then use

natcasesort(array) and here is the link http://php.net/manual/en/function.natcasesort.php

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

3 Comments

can this sort alphabetically as well as case? @agha-umair-ahmed
No It does not take care of case as you want
after sorting with netcasesort() i get this again this test dallas University Texas Dallas University Texas Johnson Johnson Johnson Johnson test again this test This Texas dallas University Texas Dallas University This test this test again University Texas dallas University Texas Dallas. the correct output should be this again this test dallas University Texas Dallas University Texas Johnson Johnson test again this test This this test again Texas dallas University Texas Dallas University This test University Texas dallas University Texas Dallas @agha-umair-ahmed
0

You need to use natcasesort(array);

I am getting this result for applying natcasesort on your array:

natcasesort gives following output:

 Array
 (
     [2] => again this test
     [8] => dallas University Texas
     [5] => Dallas University Texas
     [11] => Johnson Johnson
     [12] => Johnson Johnson
     [3] => test again this
     [0] => test This
     [9] => Texas dallas University
     [6] => Texas Dallas University
     [1] => This test
     [4] => this test again
     [10] => University Texas dallas
     [7] => University Texas Dallas
 )

natsort() gives following output:

 Array
 (
     [5] => Dallas University Texas
     [11] => Johnson Johnson
     [12] => Johnson Johnson
     [6] => Texas Dallas University
     [9] => Texas dallas University
     [1] => This test
     [7] => University Texas Dallas
     [10] => University Texas dallas
     [2] => again this test
     [8] => dallas University Texas
     [0] => test This
     [3] => test again this
     [4] => this test again
 )

9 Comments

this cannot sort all the values alphabetically @madforstrength
I have applied natcasesort on your array isnt it the result you want?
no . i want the index[4] to come before [9] because small case t should come before all T @madforstrength
the small case should come first
this is what i want Array ( [2] => again this test [8] => dallas University Texas [5] => Dallas University Texas [11] => Johnson Johnson [12] => Johnson Johnson [3] => test again this [0] => test This [4] => this test again [9] => Texas dallas University [6] => Texas Dallas University [1] => This test [10] => University Texas dallas [7] => University Texas Dallas )
|
0

Try this...

     <?php
        $cars = array("test This", "This test", "again this test","test again this"
,"this test again","Dallas University Texas","Texas Dallas University","University Texas Dallas","dallas University Texas","Texas dallas University","University Texas dallas",
"Johnson Johnson","Johnson Johnson");
        arsort($cars);
        natcasesort($cars);
        print_r($cars);
        ?>

Result:

     Array ( [2] => again this test [8] => dallas University Texas 
[5] => Dallas University Texas [12] => Johnson Johnson [11] => Johnson Johnson 
[3] => test again this [0] => test This [6] => Texas Dallas University 
[9] => Texas dallas University [1] => This test [4] => this test again 
[7] => University Texas Dallas [10] => University Texas dallas )

Reference Link: http://php.net/manual/en/array.sorting.php

3 Comments

this cannot sort all the values alphabetically correctly in order, it does well with case @deena
this is what i want Array ( [2] => again this test [8] => dallas University Texas [5] => Dallas University Texas [11] => Johnson Johnson [12] => Johnson Johnson [3] => test again this [0] => test This [4] => this test again [9] => Texas dallas University [6] => Texas Dallas University [1] => This test [10] => University Texas dallas [7] => University Texas Dallas ) @deena
' Array ( [2] => again this test [8] => dallas University Texas [5] => Dallas University Texas [11] => Johnson Johnson [12] => Johnson Johnson [3] => test again this [0] => test This [4] => this test again [9] => Texas dallas University [6] => Texas Dallas University [1] => This test [10] => University Texas dallas [7] => University Texas Dallas '@deena
0

I finally used usort() to sort the array. i have the following class with methods that constitute algorithm:

class Alphabetizer extends AbstComposite{

    private $input = array();
    private $output = array();
function Alphabetizer( $input){
    $this->input = $input;
    $this->transform();
    $this->output();
}
    static function checkcase($string) {

        if( preg_match('/[A-Z]/', $string)){
            return 1;
        }elseif(preg_match('/[a-z]/', $string)){
            return 0;
        }else{
            return "stupid";
        }
    }
  static    function caseorder($a,$b){
       $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        //checking the case ordering of words
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
                if(strcasecmp($a[$i],$b[$i] == 0)){
                    if((Alphabetizer::checkcase($a[$i]) ==0) && Alphabetizer::checkcase($b[$i]) ==1){
                        return -1;
                    }elseif(Alphabetizer::checkcase($a[$i]==1) && Alphabetizer::checkcase($b[$i] ==0)){
                        return +1;
                    }else{
                        return 0;
                    }
                }return 0;



        }
    }
    //works well for alphabetical ordering of sentences
   static  function  letterorder($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
            if(strtolower($a[$i]) < strtolower($b[$i])){
                return -1;
            }elseif(strtolower($a[$i]) > strtolower($b[$i])){
                return +1;
            }else{
                return 0;
            }

        }

    }
   static  function alphaorder($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $length= (count($a) < count($b)) ?  count($a) :count($b);
        for($i=0;$i<=$length;$i++){
            if(strtolower($a[$i]) > strtolower($b[$i])){
                return 1;
            }elseif(strtolower($a[$i]) < strtolower($b[$i])){
                return -1;
            }


        }
    }


   static  function comparator($a,$b){
        $a = preg_replace('/\s+/','',trim($a));
        $b = preg_replace('/\s+/','',trim($b));
        $return_value=0;
     if(($return_value=Alphabetizer::letterorder($a,$b))==0){
         if(($return_value=Alphabetizer::caseorder($a,$b) )== 0){
             if(($return_value=Alphabetizer::alphaorder($a,$b)) == 0){
               if(strlen($a) < strlen($b))  {
                 return 1;
               }else{
                   return 0;
               }
             }else{return$return_value;}
         }else{ return $return_value;}

     }else{
         return $return_value;
     }

    }


    public function transform(){
  usort($this->input,array(&$this,"Alphabetizer::comparator"));

    }
    public function output(){
        return array_unique($this->input);
    }

} 

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.