How is array sorting done?

Often, when solving certain problems, it is necessary to sort the data that is stored in the array. What is array sorting? For example, when playing preference, people lay out their cards by value and suit. This makes it possible to determine what other cards they are missing. And in the dictionaries, everything is sorted alphabetically. There are many examples. Sorting is a rearrangement of a certain set of objects in any order according to a given attribute. Array sorting is required quite often. For this, different methods are used. To understand their essence, it is enough to consider in detail several ways.

Array Sorting
Based on what they are doing

It is important to understand that an array consists of numerous key pairs and specific values. Sorting arrays in the C language is done using dozens of lines of code, and in the PHP language this is achieved only with one simple command. Arrays can be sorted based on keys or values. You can also distribute values ​​by leaving them existing keys or assigning new ones.

The main differences of functions

PHP array sorting is possible using various functions. Let's look at how they differ:

- Some functions sort arrays by the keys of their elements, while others - by values.

- There are different sorting orders: decreasing, increasing, natural, numerical, alphabetical, user-defined or random.

- Some functions are able to preserve after sorting the relationship that exists between the key and value. But there are functions in which keys are reset to new values.

- Each function modifies the transferred array. They do not return a sorted copy.

- The sort order is considered undefined when the function defines two elements as equal. This is an unstable sort.

Some array sorting functions in PHP

The functions sort () and rsort () . Sort () alphabetizes the array. Please note: this function is case sensitive. Sorting by values ​​without taking into account the keys. Rsort () sorts in the reverse order also by value and does not consider keys.

Asort () is one of the functions that preserves the relationship of keys and values. It is useful for associative arrays when it is important.

PHP array sorting
In the example, the keys are the names of the fruits, and the values ​​are the prices. Sorting occurs in ascending order of price. If sorting by fruit names is needed , then you need the ksort () function, which does sorting by key. Arsort () sorts one-dimensional arrays with indices (descriptive) in descending order of values. Krsort () sorts descending key elements.

Two-dimensional array

Sorting a two-dimensional array is interesting. This can be done in many ways. PHP has the ability to compare two numbers or two strings. But in any multidimensional array, each of the elements is an array. In PHP, to compare multiple arrays, you need to create a specific method. Consider a two-dimensional array in which the abbreviated name of the fruit, the full name and price are stored. Array elements can be sorted alphabetically by abbreviated names.

Sort a two-dimensional array
In the example, our function has the name compare (comparison). She has 2 arguments - x, y. The function should take 2 values, and then determine the order. Parameters x, y are 2 arrays that are inside y of the main array. To compare description elements from arrays that are passed to the function, the variables $ x [1], $ y [1] are needed. In the line return1, the value is returned to the code that called the function. The sorting of our array is based on the usort () function. Sorting follows the rules described by the compare () function.

Now sorting arrays in PHP will become clear to you.

Source: https://habr.com/ru/post/K7554/


All Articles