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: Build in API Sort Function (mivascript)

Scot Ranney • December 19, 2023


Example: List all pages, sort by code.


  <div class="form-group">
	<label>Email Template</label>
	<MvASSIGN NAME = "l.ok" VALUE = "{ [ g.module_feature_tui_db ].PageList_Load_All(l.pages) }" />
	<MvASSIGN NAME = "l.ok" VALUE = "{ [ g.Module_Library_Utilities ].QuickSortArray(l.pages,':code',1) }" />
	<select name="_customerupload_notify_email_template" class="form-control col-3">
		<MvFOREACH ITERATOR = "l.page" INDEX = "l.pos" ARRAY = "l.pages">
				<MvIF EXPR = "{ l.page:id EQ g.customerupload_notify_email_template }">
					<option value="{ l.page:id }" selected><MvEVAL EXPR = "{ l.page:name $ ' (' $ l.page:code $ ')' }">
				<MvELSE>
					<option value="{ l.page:id }"><MvEVAL EXPR = "{ l.page:name $ ' (' $ l.page:code $ ')' }">
				</MvIF>
		</MvFOREACH>
	</select>
</div>

Create a unique sort field by rolling through the array initially and setting up a slugified sort field.

<MvASSIGN NAME = "l.ok" VALUE = "{ [ g.module_feature_tui_db ].PageList_Load_All(l.pages) }" />

<MvFOREACH ITERATOR = "l.page" INDEX = "l.pos" ARRAY = "l.pages">
	<mvt:assign name="l._temp_array" value="l.page" />
	<mvt:assign name="l._temp_array:sort" value="slugify(substring(l.page:name,1,2) $ ' ' $ l.page:id $ ' ' $ l.page:code)" />
	<mvt:assign name="l.ok" value="miva_array_insert(l.settings:new_array,l._temp_array,-1)" />
</MvFOREACH>

//OK, this is smt code, got confused, but the idea is the same.

  <div class="form-group">
	<label>Email Template</label>

	<MvASSIGN NAME = "l.ok" VALUE = "{ [ g.Module_Library_Utilities ].QuickSortArray(l.settings:new_array,':sort',1) }" />

	<select name="_customerupload_notify_email_template" class="form-control col-3">
		<MvFOREACH ITERATOR = "l.page" INDEX = "l.pos" ARRAY = "l.settings:new_array">
				<MvIF EXPR = "{ l.page:id EQ g.customerupload_notify_email_template }">
					<option value="{ l.page:id }" selected><MvEVAL EXPR = "{ l.page:name $ ' (' $ l.page:code $ ')' }">
				<MvELSE>
					<option value="{ l.page:id }"><MvEVAL EXPR = "{ l.page:name $ ' (' $ l.page:code $ ')' }">
				</MvIF>
		</MvFOREACH>
	</select>
</div>

https://www.scotsscripts.com/mvblog/sort-build-in-api-sort-function-mivascript.html

mvkb_mivascript mvkb_sort