Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to sort drop down values

Posted on 2014-12-09
3
Medium Priority
?
82 Views
Last Modified: 2014-12-14
Hi i have drop down value


like <select name="x" id="x">
                <option selected="selected">Sathish</option>
             <option >vijay</option>
              <option >Fox</option>
                 <option >Ajith</option>
 <option >David</option>
</selected>

how to sort this drop down ?
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 2000 total points
ID: 40488658
Well, if you're using jQuery, then you can use one of the multitude of examples on the web. I've used this one here:

http://codepen.io/AlexBezuska/pen/kCwvJ

var sortSelect = function (select, attr, order) {
    if(attr === 'text'){
        if(order === 'asc'){
            $(select).html($(select).children('option').sort(function (x, y) {
                return $(x).text().toUpperCase() < $(y).text().toUpperCase() ? -1 : 1;
            }));
            $(select).get(0).selectedIndex = 0;
            e.preventDefault();
        }// end asc
        if(order === 'desc'){
            $(select).html($(select).children('option').sort(function (y, x) {
                return $(x).text().toUpperCase() < $(y).text().toUpperCase() ? -1 : 1;
            }));
            $(select).get(0).selectedIndex = 0;
            e.preventDefault();
        }// end desc
    }

};

//, attr, order
$(document).ready(function () {

    $('#btnSort').click(function (e) {

        sortSelect('#x', 'text', 'asc');

    }); // event listener click

});

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40488660
unless the data comes from a db query and you build the list dynamically, the only way to sort is to sort them in the html code as such. maybe you could find some javascript to do this "after the page loaded", but I would highly recommend against such a technique...
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40488869
If you use underscorejs you can do it with as little code as this:
var options = $('#x option');
var sorted = _.sortBy(options, function(item){
  return item.innerHTML;
});
$('#x').html(sorted);

Open in new window


Here's a working demo: http://plnkr.co/edit/re5RKeN2UcRAf7WRuxa5?p=preview

Although if you have the option, get it sorted right out of the database.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question