How To Sort A List With Symbols In Js
I have a drop down with a list of options which looks like this:
Solution 1:
Extract the text (exclude the 1st one), sort and swap out the text using the sorted array
With HTML
<selectid="weightClass"class="form-control input-sm"><optionvalue="Default">Please Select</option><option><200</option><option>>200</option><option>200</option><option>Unknown</option></select>
you can use this script (after the above HTML)
var sortOrder = {
'<': 1,
// placeholder for the value'>': 3,
'U': 4
}
functionsortDropDown(target) {
// get textvar opts = [];
$(target + " option").each(function (i) {
if (i)
opts.push($(this).text())
});
opts.sort(function (a, b) {
// get the sort order using the first charactervar oa = sortOrder[a[0]] || 2;
var ob = sortOrder[b[0]] || 2;
if (oa > ob)
return1;
elseif (oa < ob)
return -1;
elsereturn0;
});
// change the option text
$(target + " option").each(function (i) {
if (i)
$(this).text(opts[i - 1])
});
}
sortDropDown("#weightClass")
var sortOrder = {
'<': 1,
'>': 3,
'U': 4
}
functionsortDropDown(target) {
var opts = [];
$(target + " option").each(function (i) {
if (i)
opts.push($(this).text())
});
console.log(opts)
opts.sort(function (a, b) {
var oa = sortOrder[a[0]] || 2;
var ob = sortOrder[b[0]] || 2;
if (oa > ob)
return1;
elseif (oa < ob)
return -1;
elsereturn0;
});
$(target + " option").each(function (i) {
if (i)
$(this).text(opts[i - 1])
});
}
sortDropDown("#weightClass")
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="weightClass"class="form-control input-sm"><optionvalue="Default">Please Select</option><option><200</option><option>>200</option><option>200</option><option>Unknown</option></select>
Solution 2:
You should use a sorting function to do the special sort that you need. Here is the sorting function with an example of how to use it:
var t = ['some other option', '100', '120', '300', '>300', '<300', '<100', '>400', '200', '<200', '>200', 'another special option']
function specialSort(a, b) {
var newA, newB;
if (a[0] == '<' || a[0] == '>') {
newA = parseInt(a.substr(1))
} elseif (!isNaN(a)) {
newA = parseInt(a)
} else {
newA = null;
}
if (b[0] == '<' || b[0] == '>') {
newB = parseInt(b.substr(1))
} elseif (!isNaN(b)) {
newB = parseInt(b)
} else {
newB = null;
}
if (newA == null) {
return1;
}
if (newB == null) {
return -1;
}
if (typeof(newA) == 'number' && typeof(newB) == 'number') {
if (newA < newB) {
return -1;
} elseif (newA > newB) {
return1;
} elseif (newA == newB) {
if (a[0] == '<') {
return -1;
} elseif (b[0] == '<') {
return1;
} elseif (a[0] == '>') {
return1
} elseif (b[0] == '>' ) {
return -1;
}
}
}
return0;
}
console.log(t.sort(specialSort));
// Output is ["<100", "100", "120", "<200", "200", ">200", "<300", "300", ">300", ">400", "another special option", "some other option"]
You can use jQuery to get the values of your SELECT tag and sort them using this:
valuesToSort = []
$('#weightClass').find('option').each(function(){r.push($(this).text())})
valuesToSort.sort(specialSort)
Now the array valuesToSort
is sorted the way you want and you can put the values back into your #weightClass
tag.
Post a Comment for "How To Sort A List With Symbols In Js"