How To Sort A List With Symbols In Js July 09, 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 Detecting Individual Unicode Character Support With JavaScript Is it possible to detect if the client supports a particula… Inconsistant Date Parsing With Missing Timezone In doing some testing I've found inconsistant behavior … Cut,copy And Paste Is Not Working For Firefox 15 Onwords? I am using the netscape.security.PrivilegeManager.enablePri… JavaScript Watch Event Not Working On DOM Element Objects? I know I can use watch to bind a callback that will be trig… Browser & OS As Body Class I would like to have the OS and the Browser in the body cla… Change Url Location On Click The Div I want to change location on clicking the div . Below is co… How To Find The Nearest Common Ancestors Of Two Or More Nodes? Users selects two or more elements in a HTML page. What i w… Keep Track Of How Much Time Is Spent Showing Certain Elements On The Page So lets say we have 4 Divs (3 hidden, 1 visible), the user … Send Message To Background Page, Update The Html, And Then Show It In A Tab I have a chrome extension where a user inputs some informat… How To Set An Extjs Grid Record Phantom To True In Ext 4.1, I am dropping items to a grid, but the records … December 2024 (1) November 2024 (39) October 2024 (64) September 2024 (23) August 2024 (370) July 2024 (337) June 2024 (712) May 2024 (1296) April 2024 (801) March 2024 (1570) February 2024 (1703) January 2024 (1354) December 2023 (1311) November 2023 (407) October 2023 (622) September 2023 (314) August 2023 (349) July 2023 (275) June 2023 (358) May 2023 (222) April 2023 (134) March 2023 (143) February 2023 (169) January 2023 (255) December 2022 (127) November 2022 (239) October 2022 (160) September 2022 (167) August 2022 (495) July 2022 (297) June 2022 (279) Menu Halaman Statis Beranda © 2022 - JavaScript Charts