Struggling getting Jquery sort and unique to work

Hi,
    I have a dropdown html select box, which lists some values from an array pulled in from MySql, the following jquery code sorts this select dropdown into alphabetical order, here's the code:

$(document).ready(function() { 

    var options = $('select.whatever option');
    var arr = options.map(function(_, o) {
        return {
            t: $(o).text(),
            v: o.value
        };
    }).get();
    arr.sort(function(o1, o2) {
        return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0;
    });
    options.each(function(i, o) {
        console.log(i);
        o.value = arr[i].v;
        $(o).text(arr[i].t);
    });
});

Open in new window


The dropdown select box is called "Whatever" - this works great in that they are sorted, however I now want to only list the unique values.  How can I edit the Jquery code so no duplicates exist?

Thanks
damianb123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Question - why are you not sorting this using your db query?
0
Julian HansenCommented:
After reading your question again why not pull the distinct values and sort them when you pull them from the DB
Something like this?
SELECT DISTINCT(listboxvalue) FROM table ORDER BY listboxvalue

Open in new window

0
Julian HansenCommented:
If you really want to do this then all you need to do (given your array is sorted) - is to keep track of the last value inserted and then only add another one if it is different, so ....
var lastvalue = '';
options.each(function(i, o) {
  console.log(i);
  if (lastvalue != arr[i].v]) {
    o.value = arr[i].v;
    $(o).text(arr[i].t);
    lastvalue = arr[i].v;
  }
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

damianb123Author Commented:
Hi julianH,
      I can't do the distinct against the SQl statement, due to the way the select statement is created via an exploded array to separate comma separated values.....  I tried this, but couldn't get it to work like this:

$(document).ready(function() { 

    var options = $('select.whatever option');
	var lastvalue = '';
    var arr = options.map(function(_, o) {
        return {
            t: $(o).text(),
            v: o.value
        };
    }).get();
    arr.sort(function(o1, o2) {
        return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0;
    });
    options.each(function(i, o) {
        console.log(i);
        o.value = arr[i].v;
        $(o).text(arr[i].t);
    });
	
	var lastvalue = '';
options.each(function(i, o) {
  console.log(i);
  if (lastvalue != arr[i].v]) {
    o.value = arr[i].v;
    $(o).text(arr[i].t);
    lastvalue = arr[i].v;
  }
});
});

Open in new window


Can you help?

Thanks
0
Julian HansenCommented:
When you say it does not work - can you give more details?

The code posted does work in the tests I have done here. What happens on your side - do you get an error?

Do you have a link to a page where we can see the code in action?
0
damianb123Author Commented:
Hi julianH,
      With the above code added, all it does is removes the sorting, and jumbles all the values up - so both the sort and unique don't work.  This is the full page code, without your code - this works just as a sort, but lists the duplicates.  Can you show me where your new code should be added?

<?php			
mysql_connect('localhost', 'cst13', 'V7]!8S1AP8');
mysql_select_db('cst13_db');

$sql = "SELECT Doctors FROM maindata WHERE Doctors != ''";
$result = mysql_query($sql) or die(mysql_error()); 

?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script type='text/javascript'>



$(document).ready(function() { 

    var options = $('select.whatever option');
    var arr = options.map(function(_, o) {
        return {
            t: $(o).text(),
            v: o.value
        };
    }).get();
    arr.sort(function(o1, o2) {
        return o1.t > o2.t ? 1 : o1.t < o2.t ? -1 : 0;
    });
    options.each(function(i, o) {
        console.log(i);
        o.value = arr[i].v;
        $(o).text(arr[i].t);
    });
});

</script>

<?php
echo "<select class='whatever' name='DoctorID' size='1' style='font-family: Arial; font-size: 20pt; border:solid 1px #000000; border-width: 1px'>";
   echo "<option selected='selected' value='allgps'>-- See all GPs --</option>";
   while ($list = mysql_fetch_assoc($result)) {
  
	$doctors = array_filter( explode(",", $list['Doctors']), 'strlen' );
     
      foreach($doctors as $docs) {
		  
         echo "<option value='$docs'>$docs</option>";
      } 

   } 
echo "</select>";
?>

Open in new window


You can see the page working with the sort, here:

http://www.comparedrs.com/dropdown.php

Thanks for your help.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.