I need help sorting through some data.
Say I type “piz” in a searchfield. I get in return and array with all the entries that contain “piz”.
I now want to display them in the following order:
pizza
pizzeria
apizzetto
berpizzo
First the items that start with what I typed in alphabetical order then the ones that contain what I typed in alphabetical order.
Instead if I sort them alphabetically I get the following
apizzetto
berpizzo
pizza
pizzeria
Does anyone know how to do this?
Thanks for your help.
You can split the data into two arrays, one that starts with your input and one that doesn’t. Sort each separately, then combine the two results:
You can see it work here: http://jsfiddle.net/jfriend00/nH2Ff/