Tuesday, February 27, 2007

Sorting (Again)- but this time, easyer


The last post was about posting too but there I presented a general, well known sorting algorithm(Bubble Sort) that sorts a string of numbers.
PHP has a simple function that does a lot of things in the sorting world but comparing to Bubble Sort, You don't need to see and understand the source code.
The function looks like this:

array_multisort();

and it can sort 1 or even 2 dimensional arrays....
you can use it by giving values like: the array, sorting order, sorting type.

array_multisort($array,sorting order, sorting type);

The array option is recommended but the other 2 are optional....
For the sorting order, you have the following options:
  1. SORT_ASC - sort in descending order (ex: from A to Z)
  2. SORT_DESC - sort in descending order (ex: from Z to A)
For the sorting Type you have other options:

  1. SORT_REGULAR - This option compares elements in a normal way
  2. SORT_NUMERIC - This option compares elements as numeric values
  3. SORT_STRING - This option compares elements as string values
You can optionally add more arrays...

That's about it... You just call the function. and set the objects. It does everything...FAST MAGIC :)

No comments: