Sorting is any process of arranging items in some sequence and/or in different sets.
It aids in the process of organizing data into a presentable format.

There are many sorting algorithms in existance .
(see them in action at : http://www.sorting-algorithms.com ).
PHP uses Quick Sort in implementing most types of sorting.

PHP offers a variety of options by which 1 can sort data into a number of formats.
A look at some regularly used sorting functions :

Legend : Green indicates Optional Values.

  1. sort() :
    • Syntax : sort ( $array,$sort_flags)
    • Action : Elements will be arranged from lowest to highest when this function has completed.
    • Returns : Boolean (TRUE or FALSE) depending on success
    • $sort_flags values :
      • SORT_REGULAR – Default Mode.compare items normally (don’t change types)
      • SORT_NUMERIC – compare items numerically
      • SORT_STRING – compare items as strings
    • Keys are lost after sort() is used.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : sort($fruits);
      Output Array :
      fruits[0] = apple
      fruits[1] = banana
      fruits[2] = lemon
      fruits[3] = orange
    • When this function is used for a 2 D Array, then it sorts by the value in the 1st index of the level 2 of the array(below eg it will sort by value in column ‘data1’ & only the indices in the 1st level will be lost.
    • Eg :
      Input 2 D Array :
      fruits[‘a’][‘data1’] = “222”
      fruits[‘a’][‘data2’] = “123”
      fruits[‘b’][‘data1’] = “111”
      fruits[‘b’][‘data2’] = “456”
      Action : sort($fruits);
      Output Array :
      fruits[0][‘data1’] = “111”
      fruits[0][‘data2’] = “456”
      fruits[1][‘data1’] = “222”
      fruits[1][‘data2’] = “123”
  2. asort() :
    • Syntax : asort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that keys are not lost.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : asort($fruits);
      Output Array :
      fruits[‘d’] = apple
      fruits[‘c’] = banana
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
  3. rsort() :
    • Syntax : rsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that this function sorts an array in reverse order (highest to lowest).
    • Keys are lost after sort() is used.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : rsort($fruits);
      Output Array :
      fruits[0] = orange
      fruits[1] = lemon
      fruits[2] = banana
      fruits[3] = apple
    • When this function is used for a 2 D Array, then it sorts by the value in the 1st index of the level 2 of the array(below eg it will sort by value in column ‘data1’ & only the indices in the 1st level will be lost.
    • Eg :
      Input 2 D Array :
      fruits[‘a’][‘data1’] = “111”
      fruits[‘a’][‘data2’] = “456”
      fruits[‘b’][‘data1’] = “222”
      fruits[‘b’][‘data2’] = “123”
      Action : sort($fruits);
      Output Array :
      fruits[0][‘data1’] = “222”
      fruits[0][‘data2’] = “123”
      fruits[1][‘data1’] = “111”
      fruits[1][‘data2’] = “456”
  4. arsort() :
    • Syntax : arsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned rsort() function except that keys are not lost.
    • Eg :
      Input Array :
      fruits[‘a’] = lemon
      fruits[‘b’] = orange
      fruits[‘c’] = banana
      fruits[‘d’] = apple
      Action : arsort($fruits);
      Output Array :
      fruits[‘b’] = orange
      fruits[‘a’] = lemon
      fruits[‘c’] = banana
      fruits[‘d’] = apple
  5. ksort() :
    • Syntax : ksort ( $array,$sort_flags)
    • Exactly similar to the above mentioned sort() function except that here it Sorts an array by key, maintaining key to data correlations.
    • Eg :
      Input Array :
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘a’] = banana
      fruits[‘d’] = apple
      Action : ksort($fruits);
      Output Array :
      fruits[‘a’] = banana
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘d’] = apple
  6. krsort() :
    • Syntax : krsort ( $array,$sort_flags)
    • Exactly similar to the above mentioned ksort() function except that here it Sorts an array by key in reverse order, maintaining key to data correlations.
    • Eg :
      Input Array :
      fruits[‘b’] = lemon
      fruits[‘c’] = orange
      fruits[‘a’] = banana
      fruits[‘d’] = apple
      Action : krsort($fruits);
      Output Array :
      fruits[‘d’] = apple
      fruits[‘c’] = orange
      fruits[‘b’] = lemon
      fruits[‘a’] = banana
Summary of Sorting Functions in PHP
Function name Sorts by Maintains
Key Relation
Order of sort
array_multisort() value associative yes,

numeric no

first array or sort options
asort() value yes low to high
arsort() value yes high to low
natcasesort() value yes natural, case insensitive
natsort() value yes natural
rsort() value no high to low
shuffle() value no random
sort() value no low to high
uasort() value yes user defined
usort() value no user defined
krsort() key yes high to low
ksort() key yes low to high
uksort() key yes user defined


Ref :
http://us.php.net/manual/en/array.sorting.php


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s