0

I have a drop-down list like below, which contains the version IDs in the id tag of options:

Code:

$(document).ready(function() {
  $("#ddlList").bind("click", function(e) {
    $("#ddlList").html($('#ddlList option').sort(function(x, y) {
      var a = x.id;
      var b = y.id;
      return a > b ? -1 : 1;
    }))
    $("#ddlList").get(0).selectedIndex = 0;
    e.preventDefault();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="ddlList">
  <option id="10.1.0" value="3">B</option>
  <option id="2.8.9" value="1">H</option>
  <option id="5.6.7." value="1">G</option>
  <option id="10.0.1" value="1">C</option>
  <option id="1" value="1">I</option>
  <option id="10.1.2" value="1">A</option>
  <option id="7" value="1">E</option>
  <option id="9.2.5" value="2">D</option>
  <option id="6" value="8">F</option>
</select> <br/><br/> <input type="button" id="btnSortVal" Value=" Sort Dropdown By Value " />

The values aren't sorted properly. Upon clicking on the drop-down list, I need to sort the values in descending order.

Can someone help me with this?

Desired Output : A,B,C,D,E,F,G,H,I

Actual Output: D,E,F,G,H,A,B,C,I

Not A Bot
  • 2,474
  • 2
  • 16
  • 33
Sanjay
  • 33
  • 1
  • 7
  • No, I need to convert these values into array first then again I need to sort and store those back into list. It's way to complex solution. Seen this before however – Sanjay Feb 08 '21 at 12:39
  • @freedomn-m If you don't know the solution, why do you close the question? – Sanjay Feb 08 '21 at 12:44
  • I have no clue why it got closed, he didn't only asked for sorting dotted numbers but however: @Sanjay Here is the Solution for you https://pastebin.com/ghSHFgUR – Scriptkiddy1337 Feb 08 '21 at 13:24
  • @RenéDatenschutz this link isn't opening – Sanjay Feb 08 '21 at 13:39

0 Answers0