Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone

In this part of ‘How to use Linq in PHP’ we’re going to look at variety different ways we could sort given array or collection. Yalinqo has very powerful set of functions to perform sorting easily, otherwise we may have to write our own functions and loops doing so. Here I’m going to start with very basic form of sorting and then move towards explaining more complex and dynamic ways supported by Yaliqo.

Simple Sorting

First we will see how to sort simple one dimensional array in both ascending and descending order. Yalinqo provides two OrderBy function to sort in ascending order and orderByDescending function for descending order. Following code snippet will illustrate the usage of these two functions.

I assume now you have some understanding of how to use Yalinqo library so the above code snippet may not need explanation on how to use it. We simply create Enumerable object and then call orderBy and/or orderByDescending in order to sort the array ascending or descending order. When you run the snippet you will see following output.

Following example will show you that we could use string arrays as well.

 Sorting Associative Arrays

Let’s see how to sort associative arrays. If you remember about two special literals $k and $v that we discussed in previous post, this going to be easy to understand. We can specify whether to sort by keys or by values in the associative array. Following example will show you different usage of these two literals in orderBy function.

If you look at the $data2 associative array and the line with $orderByKeyWithValue = $data2->orderBy('$k+$v')->toArray(); you will understand that we even can use both key and value together in orderBy function to create different sorting logics. Here the array is order by sum value of both key and value.

Order by Custom Comparer

You can also customise the sorting logic by giving your own comparer function to orderBy. Here you define a comparer which takes two objects from your array and decide which one is larger than the other, then Yalinqo will use it to sort the elements. Try to understand following code snippet.

Note that in here we want to sort by the string length of the key. So we define a function which takes two elements and decide which element has the longest string. Then we pass this function to orderBy. To illustrate the difference I first order by key alone and then the same array using our custom comparer.

Decide Order Direction Dynamically

Sometimes we may need to order by ascending or descending by use preference. For ex: if we have list of products with prices and user may want to order by low to high price and or high to low just by selecting it from drop down menu. We could write if then else to check the value and use orderBy or orderByDescending accordingly. But there are easier way to change the direction of sorting just by passing boolean value (or expression which evaluated to boolean value).

Simply passing false or true we can sort ascending or descending.

Posts of this series
How to use Linq in PHP - Part 01
How to use Linq in PHP - Part 02 - Setup
How to use Linq in PHP - Part 03 - Basic Operations
How to use Linq in PHP - Part 04 - Basic Operations
How to use Linq in PHP – Part 05 – Sorting
Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone