How To Sort A List With Symbols In Js August 06, 2024 Post a Comment I have a drop down with a list of options which looks like this: Please SelectSolution 1: Extract the text (exclude the 1st one), sort and swap out the text using the sorted arrayWith 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>Copyyou 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") Copyvar 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")Copy<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>CopySolution 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"]CopyYou 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) CopyNow the array valuesToSort is sorted the way you want and you can put the values back into your #weightClass tag. Share Post a Comment for "How To Sort A List With Symbols In Js" Top Question Edit A Txt File Using Javascript/jquery I want to modify a .txt (overwrite completely) using javasc… How To Include Highcharts Motion Plugin For Bubble Plot Using R Wrapper? Highcharts motion plugin - Requires 3 adjustments to a high… Using A Jquery Slider For Text Instead Of Images? This may be a little too specific, but I have a jquery slid… Detect Child Node Number I'm trying to add a event listener for clicking and I w… Fade In Fade Out Using Dynamic Range Input Speed And Repeat am Trying to control the speed and repeat of my fade in fad… Genvalidator: Check For Checkbox In Form Validation I'm using genvalidator to have some tests on input fiel… Text-overflow Ellipsis Without "white-space: Nowrap"? I was wondering if there is any clever way, to achieve the … How To Handle 'Possibly Unhandled Rejection: Backdrop Click' In A General Way I have an angular service for handling modals: angular.modu… Call Multiple Ajax Request In Series Or Parallel I have 3 functions. load_graphs('national','… Axios - How To Deal With Big Integers Here is my request: axios.get(url) .then(res => … December 2024 (1) November 2024 (37) October 2024 (60) September 2024 (18) August 2024 (382) July 2024 (342) June 2024 (695) May 2024 (1295) April 2024 (805) March 2024 (1554) February 2024 (1686) January 2024 (1347) December 2023 (1310) November 2023 (414) October 2023 (567) September 2023 (315) August 2023 (341) July 2023 (278) June 2023 (374) May 2023 (204) April 2023 (124) March 2023 (136) February 2023 (187) January 2023 (261) December 2022 (123) November 2022 (224) October 2022 (150) September 2022 (164) August 2022 (488) July 2022 (285) June 2022 (263) Menu Halaman Statis Beranda © 2022 - JavaScript College