Php-specific array sorting class ArraySortUtil usage example

Source: Internet
Author: User
This article mainly introduces the usage of php-specific array sorting class ArraySortUtil. The example analyzes the methods and corresponding skills of ArraySortUtil for Array sorting. For more information, see

This article mainly introduces the usage of php-specific array sorting class ArraySortUtil. The example analyzes the methods and corresponding skills of ArraySortUtil for Array sorting. For more information, see

This article describes the usage of php-specific array sorting class ArraySortUtil. Share it with you for your reference. The details are as follows:

* ** ArraySortUtil is a array sort utility, you can extends the sorting engine. ** @ version 0.1 * @ package utility. sort */class ArraySortUtil {static function uasort ($ unsort, $ fields) {if (! Is_array ($ unsort) | sizeof ($ unsort) <= 0) return $ unsort; $ sorted = uasortEngine: uasort ($ unsort, $ fields); return $ sorted ;} static function multisort ($ unsort, $ fields) {if (! Is_array ($ unsort) | sizeof ($ unsort) <= 0) return $ unsort; $ sorted = multisortEngine: multisort ($ unsort, $ fields ); return $ sorted ;}} class multisortEngine {static function multisort ($ unsort, $ fields) {$ sorted = $ unsort; if (is_array ($ unsort )) {$ loadFields = array (); foreach ($ fields as $ sortfield) {$ loadFields ["field"] [] = array ("name" => $ sortfield ["field"], "order" => $ sortfield ["order"], "nature" => $ Sortfield ["nature"], "caseSensitve" => $ sortfield ["caseSensitve"]); $ loadFields ["data"] [$ field ["field"] = array ();} // Obtain a list of columnsforeach ($ sorted as $ key => $ row) {foreach ($ loadFields ["field"] as $ field) {$ value = $ row [$ field ["name"]; $ loadFields ["data"] [$ field ["name"] [$ key] = $ value ;}$ parameters = array (); foreach ($ loadFields ["field"] as $ sortfield) {$ array_data = $ loadFields ["dat A "] [$ sortfield [" name "]; $ caseSensitve = ($ sortfield [" caseSensitve "] = null )? $ Sortfield ["caseSensitve"]: false; if (! $ CaseSensitve) $ array_data = array_map ('strtolower', $ array_data); $ parameters [] = $ array_data; if ($ sortfield ["order"]! = Null) $ parameters [] = ($ sortfield ["order"])? SORT_DESC: SORT_ASC; if ($ sortfield ["nature"]! = Null) $ parameters [] = ($ sortfield ["nature"])? SORT_REGULAR: SORT_STRING;} $ parameters [] = & $ sorted; call_user_func_array ("array_multisort", $ parameters);} return $ sorted ;}} class uasortEngine {static private $ caseSensitve = false; static private $ sortfields = array (); static private $ sortorder = true; static private $ nature = false; static private function uasort_callback (& $ a, & $ B) {foreach (self: $ sortfields as $ sortfield) {$ _ field = $ sortfi Eld ["field"]; $ _ order = isset ($ sortfield ["order"])? $ Sortfield ["order"]: self: $ sortorder; $ _ caseSensitve = isset ($ sortfield ["caseSensitve"])? $ Sortfield ["caseSensitve"]: self ::$ caseSensitve; $ _ nature = isset ($ sortfield ["nature"])? $ Sortfield ["nature"]: self: $ nature; if ($ _ field! = "") {$ Retval = 0; if ($ _ nature) {if ($ _ caseSensitve) {$ compare = strnatcmp ($ a [$ _ field], $ B [$ _ field]);} else {$ compare = strnatcasecmp ($ a [$ _ field], $ B [$ _ field]);} else {if ($ _ caseSensitve) {$ compare = strcmp ($ a [$ _ field], $ B [$ _ field]);} else {$ compare = strcasecmp ($ a [$ _ field], $ B [$ _ field]) ;}} if ($ compare! = 0 &&! $ _ Order) $ compare = ($ compare> 0 )? -1: 1;} if ($ compare! = 0) break;} return $ compare;} static function uasort ($ unsort, $ fields) {self: $ sortfields = $ fields; $ sorted = $ unsort; uasort ($ sorted, array ('uasortengine ', 'uasort _ callback'); return $ sorted ;}}

An example is as follows:

<? Phprequire_once ('class. arraySortUtil. php '); // declare test data $ assetData [] = array ("id" => 1, "category" => "Hardware ", "subcategory" => "Personal Computer", "supplier" => "DELL", "participant" => "Vostro 1320", "purchase_price" => 2300, "other_charges" => 500); $ assetData [] = array ("id" => 2, "category" => "Hardware ", "subcategory" => "Personal Computer", "supplier" => "DELL", "participant" => "Vostro 1420", "purchase_price" => 2500, "other_charges" => 0); $ assetData [] = array ("id" => 3, "category" => "Hardware", "subcategory" => "Laptop ", "supplier" => "DELL", "participant" => "Vostro 1520", "purchase_price" => 4500, "other_charges" => 100 ); $ assetData [] = array ("id" => 4, "category" => "Hardware", "subcategory" => "Laptop ", "supplier" => "Acer", "participant" => "Apire One", "purchase_price" => 2500, "other_charges" => 200 ); $ assetData [] = array ("id" => 5, "category" => "Furniture", "subcategory" => "Table ", "supplier" => "CHEN", "participant" => "Manager Table", "purchase_price" => 1000, "other_charges" => 0 ); $ assetData [] = array ("id" => 6, "category" => "Furniture", "subcategory" => "Table ", "supplier" => "CHEN", "participant" => "Staff Table", "purchase_price" => 500, "other_charges" => 0 ); $ assetData [] = array ("id" => 7, "category" => "Furniture", "subcategory" => "Chair ", "supplier" => "CHEN", "participant" => "Staff Chair", "purchase_price" => 500, "other_charges" => 0); $ sorted = ArraySortUtil :: multisort ($ assetData, array ("field" => "category"), array ("field" => "subcategory", "order" => true) // desc); echo "multisort: result"; echo"

"; print_r($sorted);echo "
"; $ Sorted = ArraySortUtil: uasort ($ assetData, array (" field "=>" category "), array (" field "=>" subcategory ", "order" => true) // desc); echo "uasort: result"; echo"
"; print_r($sorted);echo "
";

I hope this article will help you with php programming.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.