Right Place For Right Person TM
 Interviews

# How you differentiate among sort(),assort() and Ksort()?

Question:
How you differentiate among sort(),assort() and Ksort()?

## Question:How you differentiate among sort(),assort() and Ksort()?

1) sort() This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. <?php \$fruits = array("lemon", "orange", "banana", "apple"); sort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "fruits[" . \$key . "] = " . \$val . "\n"; } ?> ----------------------------OUTPUT--------------------- fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange ------------------------------------------------------- 2) asort() This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. <?php \$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> --------------------OUTPUT------------------------ c = apple b = banana d = lemon a = orange -------------------------------------------------- 3) ksort() Sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays. <?php \$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> --------------------OUTPUT---------------------------- a = orange b = banana c = apple d = lemon ------------------------------------------------------

By:Vishal Bhatt
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

The sort() function sort the array. Element will arranged in lowest to highest when function has completed. The assort() function sort the associative arrays and where the actual element order is significant. The Ksort() function sort an array by key. This is useful for associative array.

By:Sangita
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

Sort-sorting arry <?php \$fruits = array("lemon", "orange", "banana", "apple"); sort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "fruits[" . \$key . "] = " . \$val . "\n"; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange Assort-Sort an array and maintain index association <?php \$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> The above example will output: c = apple b = banana d = lemon a = orange ksort — Sort an array by key <?php \$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> The above example will output: a = orange b = banana c = apple d = lemon

Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

Sort-sorting arry <?php \$fruits = array("lemon", "orange", "banana", "apple"); sort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "fruits[" . \$key . "] = " . \$val . "\n"; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange Assort-Sort an array and maintain index association <?php \$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); asort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> The above example will output: c = apple b = banana d = lemon a = orange ksort — Sort an array by key <?php \$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); ksort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "\$key = \$val\n"; } ?> The above example will output: a = orange b = banana c = apple d = lemon

Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

sort() -> sorting the value in ascending.d assort() -> sorting only the value. Example: \$x=array([0]=>32,[1]=>11,[2]=>10) print_r(asort(\$x); means: [2]=>10,[1]=>11,[0]=>32 Ksort() -> Sorting only key not value

By:anilsharma
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

In array we can get both key value and array value.key is refer for index,array is what we are storing.sort() use to sort in ascendending array value.arsort() user array in reverse order. ksort means key valuesare arranged in ascending order

By:janani
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys. asort() will also sort the array by its values, but it will preserve the key -> value association. The ksort() function sorts an array by the keys. The values keep their original keys. This function returns TRUE on success, or FALSE on failure.

By:
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

The sort() function sorts an array by the values. This function assigns new keys for the elements in the array. Existing keys will be removed. This function returns TRUE on success, or FALSE on failure. The asort() function sorts an array by the values. The values keep their original keys. This function returns TRUE on success, or FALSE on failure. The ksort() function sorts an array by the keys. The values keep their original keys. This function returns TRUE on success, or FALSE on failure.

By:krishna
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

bool sort ( array &\$array [, int \$sort_flags] ) This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys. Returns TRUE on success or FALSE on failure. Example 329. sort() example <?php \$fruits = array("lemon", "orange", "banana", "apple"); sort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "fruits[" . \$key . "] = " . \$val . "\n"; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange

By:pankaj
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

bool sort ( array &\$array [, int \$sort_flags] ) This function sorts an array. Elements will be arranged from lowest to highest when this function has completed. Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys. Returns TRUE on success or FALSE on failure. Example 329. sort() example <?php \$fruits = array("lemon", "orange", "banana", "apple"); sort(\$fruits); foreach (\$fruits as \$key => \$val) { echo "fruits[" . \$key . "] = " . \$val . "\n"; } ?> The above example will output: fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange

By:pankaj
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

sort() void sort(array target_array [, int sort_flags]) The sort() function sorts the target_array, ordering elements from lowest to highest value. Note that it doesn’t return the sorted array. Instead, it sorts the array “in place,” returning nothing, regardless of outcome. The optional sort_flags parameter modifies the function’s default behavior in accordance with its assigned value: • SORT_NUMERIC: Sort items numerically. This is useful when sorting integers or floats. • SORT_REGULAR: Sort items by their ASCII value. This means that B will come before a, for instance. A quick search online will produce several ASCII tables. • SORT_STRING: Sort items in a fashion that might better correspond with how a human might perceive the correct order. See natsort() for further information about this matter, introduced later in this section. Consider an example. Suppose you wanted to sort exam grades from lowest to highest: \$grades = array(42,57,98,100,100,43,78,12); sort(\$grades); print_r(\$grades); The outcome looks like this: Array ( [0] => 12 [1] => 42 [2] => 43 [3] => 57 [4] => 78 [5] => 98 [6] => 100 [7] => 100 ) It’s important to note that key/value associations are not maintained. Consider the following example: \$states = array("OH" => "Ohio", "CA" => "California", "MD" => "Maryland"); sort(\$states); print_r(\$states); Here’s the output: Array ( [0] => California [1] => Maryland [2] => Ohio )

By:sawan kumar
Date:

## Question:How you differentiate among sort(),assort() and Ksort()?

sort destroy key association asort keeps key association of values. ksost sort the key value only

By:kuber
Date:

User Name: