Solved

how to sort drop down values

Posted on 2014-12-09
3
77 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 500 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

733 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