I've an array which is being populated by options in select dropdowns.
jQuery('.wcpf-input-drop-down').each(function () {
var sel = jQuery(this);
var selected = sel.val(); // cache selected value, before reordering
var opts_list = sel.find('option');
opts_list.sort(function(a, b) { return jQuery(a).text() > jQuery(b).text() ? 1 : -1; });
sel.html('').append(opts_list);
sel.val(selected); // set cached selected value
});
The above code is from Sorting options elements alphabetically using jQuery, written by Malak George. EDIT: I've modified it to look for all instances separately.
Now, this is working fine as far as finding the instances of .wcpf-input-drop-down and sorting the options within.
However, this sorting produces ordering in this fashion: "1, 2, 26, 27, 28, 3, 31" and so on.
I would need it to sort as follows: "1, 2, 3, 26, 27, 28, 31".
I've been searching for hours and found plenty of talk on the subject, but I cannot get to where I want. I'm sure I've seen ways to accomplish what I need, but haven't been able to. I think here's one that I could use, if I'd only know how to insert that into my existing code: Sort Array Elements (string with numbers), natural sort
Could someone help me implement the sorting I wish to see into the code I have now?
Thank you.
EDIT2: Here's a pen with all the relevant data to this question: https://codepen.io/jpontinen/pen/NJrXKQ