Miva Merchant Modules and Development
Want to start an online store? We work with you from start to finish, from commerce platform to design to SEO.
Experience counts, and we have a lot.

SORT: QuickSortArray Trick for Complex Sorting With Miva_Array_Copy_Ref(...)

Scot Ranney • February 22, 2024


QuickSortArray can be used to sort by any field in the structure.

What if the sort field doesn't exist?

Scenario: Sort shipping methods from low to high while including the name so that shipping fees are grouped from via provider from low to high, such as UPS, USPS, FED EX, etc...

Note: We'll be using the miva_array_copy_ref(...) method so we don't need to create a second array.

Create a new field in the shipping methods array specifically for sorting, set up a temporary array, then move data to original array.

<mvt:assign name="l.ok" value="miva_array_copy_ref(l.settings:shippingmethods,1,miva_array_elements(l.settings:shipping_methods),l.settings:smethods,1)" />

<mvt:foreach iterator="method" array="smethods">
	<mvt:assign name="l.settings:method:sort" value="l.settings:method:name $ l.settings:method:price" />
</mvt:foreach>

<mvt:do file="g.Module_Library_Utilities" name="l.success" value="QuickSortArray(l.settings:shippingmethods, ':sort', 'asc')" />

Now when you roll through the array, the methods are sorted by the :sort field.


https://www.scotsscripts.com/mvblog/sort-tricks-for-quicksortarray.html

mvkb_smt mvkb_sort mvkb_array