Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Exakat Blog:
PHP likes sorting too much
Jan 18, 2017 @ 17:03:07

The Exakat blog has a recent post talking about how PHP likes sorting too much, that is, even in some places you're not using specific sort functions, PHP does it for you anyway.

PHP likes to sort. Of course, there is sort(), ksort() and all the cousins. But, PHP actually sorts too much. My first encounter with the problem is the infamous array_unique(). Now, this is also affecting glob() and scandir(). I’m looking for others. Until then, check your code.

He covers the functionality for each of those previously mentioned functions and what kind of sorting they're performing: array_unique, glob and scandir. He ends the post with some quick advice on how to potentially replace these auto-sorting functions and some specific functions to use instead.

tagged: sorting automatic function nonsorting

Link: https://www.exakat.io/php-likes-sorting/

Derick Rethans:
Natural Language Sorting with MongoDB 3.4
Dec 16, 2016 @ 15:28:33

Derick Rethans (of MongoDB) has posted an update to his site sharing the details about an improvement that comes with MongoDB 3.4 and is supported by the PHP driver: natural language sorting.

Arranging English words in order is simple—most of the time. You simply arrange them in alphabetical order. Sorting a set of German words, or French words with all of their accents, or Chinese with their different characters is a lot harder than it looks.

[...] Years ago I wrote about collation and MongoDB. There is an old issue in MongoDB's JIRA tracker, SERVER-1920, to implement collation so that sorting and indexing could work depending on the different sorting orders as described for each language (locale). Support for these collations have finally landed in MongoDB 3.4 and in this article we are going to have a look at how they work.

He starts off by explaining a bit about how Unicode collation works and PHP's support through the intl extension in the Collator class. He provides a code example using the class, showing the difference in sorting them first as English words then as Norwegian words. He moves into the MongoDB world and shows how the queries using this new collation support would be structured before moving back to PHP and using the MongoDB client to make the same requests. He also includes examples showing how to set the default locale, the "strength" (for the level of comparison), sorting and some interesting quirks with certain locales.

tagged: mongodb derickrethans natural language sorting tutorial driver

Link: https://derickrethans.nl/mongodb-collation-revised.html

Zend PHP Certification Blog:
PHP Sorting Functions
Dec 21, 2011 @ 17:39:06

On the "Zend PHP Certification" blog (study notes), there's sort and natsort).

In all the countless hours I’ve spent with php, I’ve maybe used three or four of these sorting functions. I really had no idea that there is a total of eleven functions used for sorting arrays. Anyway, I’m betting that it may be useful to have these memorized before I take the Zend PHP Certification Exam so here is a brief overview of each one.

He talks about the various flags that can be used in the sorting (for regular, numeric, string and locale-based string handling) and the parameters to call for normal sorting, "natural" sorting, reverse key sorting and others. You can find specifics on these array sorting methods in the PHP manual.

tagged: sorting function array natural reverse key user

Link:

Zend Developer Zone:
Paging and Sorting Data with Zend Framework, Doctrine and PEAR (part 1)
Dec 11, 2009 @ 15:43:29

On the Zend Developer Zone there's the first part of a series looking at pagination and sorting of data with the combination of the Zend Framework, Doctrine and PEAR.

When building database-backed applications, one of the important problems for a developer or user interface engineer involves making large data sets more manageable by, and therefore more useful to, application users. [...] Back in the good old days, adding pagination to a PHP application was mostly a manual task, involving offset calculations and custom query generation. In recent years, the task has become significantly simpler, mostly due to the presence of ready-made pagination components in most common frameworks.

His tutorial uses the Zend Framework, Doctrine and the Pager, MDB2 and Structures_Datagrid PEAR packages. He starts with a basic select and format kind of example to show a few lines per page. To improve it (for larger data sets) he shows how to use the Zend_Paginator to only grab the rows needed for the page. Finally, he adds in code to allow for column sorting, making it easy to reorganize the results how you'd like.

tagged: zendframework pagination sorting doctrine pear tutorial

Link:

Bjarte Karlsen's Blog:
Sorting with uasort
Oct 30, 2006 @ 19:25:00

Bjarte Karlsen has a handy tip posted to his blog today - one array function to keep in mind when doing more than just the basic sorting - uasort.

Problem: You have an array of objects or a multidimentional arrays where the key of the first array has some meaning. It could be a id or something else. You want to sort the array and maintain the key=>value association.

Some may then proceed to loop the array and sort it in a loop, however there is a easier method.

He reminds us that the fucntion allows you to take in the array and, based on a user-defined function, sort the results, keeping the association between the keys and values.

tagged: sorting uasort user defined function maintian assoication sorting uasort user defined function maintian assoication

Link:

Bjarte Karlsen's Blog:
Sorting with uasort
Oct 30, 2006 @ 19:25:00

Bjarte Karlsen has a handy tip posted to his blog today - one array function to keep in mind when doing more than just the basic sorting - uasort.

Problem: You have an array of objects or a multidimentional arrays where the key of the first array has some meaning. It could be a id or something else. You want to sort the array and maintain the key=>value association.

Some may then proceed to loop the array and sort it in a loop, however there is a easier method.

He reminds us that the fucntion allows you to take in the array and, based on a user-defined function, sort the results, keeping the association between the keys and values.

tagged: sorting uasort user defined function maintian assoication sorting uasort user defined function maintian assoication

Link:

DevShed:
Collections and Sorting Continued
Apr 05, 2006 @ 12:13:17

Previously from DevShed, they started a series on collections and sorting in PHP. Today, they've posted part two of the series that builds on that and looks at sorting algorithm examples.

This article will examine the primary sorting algorithms with code examples, and some empirical data regarding how they perform in relation to one another, as well as the size of the data set in question.

We will also create a function to fill up our collection with random data in order to test the sort algorithms with a sufficiently large data set. The sort algorithms listed above are the ones that every computer science student learns in college and are the primary sort algorithms found in real-world applications.

The sorting styles they cover include: bubble sort, heap sort, merge sort, quick sort, and shell sort. For each, they provide the code, making it a simple matter of cut and paste to make it work in your script. There's not a whole lot of documentation going along with the code in this article, but the sorting code is simple enough to understand without it.

tagged: collections sorting bubble heap merge quick selection shell collections sorting bubble heap merge quick selection shell

Link:

DevShed:
Collections and Sorting Continued
Apr 05, 2006 @ 12:13:17

Previously from DevShed, they started a series on collections and sorting in PHP. Today, they've posted part two of the series that builds on that and looks at sorting algorithm examples.

This article will examine the primary sorting algorithms with code examples, and some empirical data regarding how they perform in relation to one another, as well as the size of the data set in question.

We will also create a function to fill up our collection with random data in order to test the sort algorithms with a sufficiently large data set. The sort algorithms listed above are the ones that every computer science student learns in college and are the primary sort algorithms found in real-world applications.

The sorting styles they cover include: bubble sort, heap sort, merge sort, quick sort, and shell sort. For each, they provide the code, making it a simple matter of cut and paste to make it work in your script. There's not a whole lot of documentation going along with the code in this article, but the sorting code is simple enough to understand without it.

tagged: collections sorting bubble heap merge quick selection shell collections sorting bubble heap merge quick selection shell

Link:

DevShed:
Collections and Sorting
Mar 29, 2006 @ 13:58:03

There are features in every language that developers love to have - in PHP, there are some impressive string and array handling functions that some developers would like to carry over. Unfortunately, there are also some bits of functionality that haven't quite made it to PHP yet - namely collections. In other languages, such as Java or C#, these handy containers let you hold objects for use later. PHP lacks this kind of functionality inherently, but this new DevShed article might just have a way around it.

A collection is an object whose primary function is to store a number of like objects. An object called CarCollection may contain any number of Car objects. Collections can traditionally be accessed in the same manner as arrays, which means CarCollection[n] represents a particular Car object. This is true in C#, Java, and more - but not PHP, unfortunately. Since PHP has only recently begun to develop a package of built in objects (the SPL, Standard PHP Library), the ability to support collections in the accepted behavioral sense is very limited.

Their mission is to work with a datatype that PHP does have to simulate this kind of collection handling - arrays. They walk you through the creation of a foundation class, one that simply allows you to get and fetch from the array. Extending that makes it possible to create a customized method for sorting personal data (name).

tagged: collections sorting array non-native objects collections sorting array non-native objects

Link:

DevShed:
Collections and Sorting
Mar 29, 2006 @ 13:58:03

There are features in every language that developers love to have - in PHP, there are some impressive string and array handling functions that some developers would like to carry over. Unfortunately, there are also some bits of functionality that haven't quite made it to PHP yet - namely collections. In other languages, such as Java or C#, these handy containers let you hold objects for use later. PHP lacks this kind of functionality inherently, but this new DevShed article might just have a way around it.

A collection is an object whose primary function is to store a number of like objects. An object called CarCollection may contain any number of Car objects. Collections can traditionally be accessed in the same manner as arrays, which means CarCollection[n] represents a particular Car object. This is true in C#, Java, and more - but not PHP, unfortunately. Since PHP has only recently begun to develop a package of built in objects (the SPL, Standard PHP Library), the ability to support collections in the accepted behavioral sense is very limited.

Their mission is to work with a datatype that PHP does have to simulate this kind of collection handling - arrays. They walk you through the creation of a foundation class, one that simply allows you to get and fetch from the array. Extending that makes it possible to create a customized method for sorting personal data (name).

tagged: collections sorting array non-native objects collections sorting array non-native objects

Link:


Trending Topics: