1

I'm doing this now:

  $.ajax({
        url: 'full_db.php',
        type: 'GET',
        dataType: 'JSON',
        data: {col_name: firstSel},
        success: function(data) {    
         var full_options = [];
          $.each(data, function (i, data) {
         full_options.push(data.age);
        full_options.sort(function(a, b){
        return a.age - b.age;
     });

     $('#second_select').append("<option>" + data.age + "</option>");
  });
}
});

This appends all of the distinct ages to my select (second_select) and if I console log full_options I get this:

["55", "98", "34", "30", "45", "29", "26", "22", "37", "42", "32", "33", "36", "35", "56", "46", "25", "54", "86"]

I'm looking to get this in ascending order (e.g.: 22, 25, 26, 29,...).

What am I doing wrong here that I'm getting an unordered array?

6
  • 9
    If you are doing a DB call do the sorting in the SELECT statement. ORDER BY column_name DESC Commented Jan 21, 2015 at 21:08
  • 2
    Out of curiosity, if you are sorting them in PHP, why the extra sort in JS after the AJAX call? Commented Jan 21, 2015 at 21:11
  • 22,25,26 is not descending order. Please correct this Commented Jan 21, 2015 at 21:22
  • 1
    what am i doing wrong .. your sort function assumes an array of objects with property age but when you push data.age into array you are only pushing in a primitive value not an object Commented Jan 21, 2015 at 21:30
  • 1
    @jonmrich It happens. Sure thing, I posted an answer. Commented Jan 22, 2015 at 6:42

2 Answers 2

1

I'm providing an answer for the comment I made.

If you are doing a DB call do the sorting in the query

SELECT * from table ORDER BY column_name DESC
Sign up to request clarification or add additional context in comments.

1 Comment

No idea how I missed this...thanks for the simple solution, which obviously fixes all my sorting problems at once.
1

You better sort the data in PHP or MySQL before it gets into JavaScript.
But if you wish, you can surely sort arrays in JavaScript.

full_options.sort(); // ascending order [1,2,3]

Or,

full_options.sort();
full_options.reverse(); // descending order [3,2,1]

Here is what you performing:

full_options.sort(function(a, b){
    return a.age - b.age;
});

This is a more complex way to do it, it sorts ascending. But you perform it in wrong place.
You code should be:

 $.ajax({
     url: 'full_db.php',
     type: 'GET',
     dataType: 'JSON',
     data: {col_name: firstSel},
     success: function(data) 
     {    
          var full_options = [];
          $.each(data, function (i, data) 
          {
              full_options.push(data.age); 
              $('#second_select').append("<option>" + data.age + "</option>");
          });
          full_options.sort();
     }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.