1

i would like to know how can i sort by alphabetical order my list of username here is my code but it's not working. I re)edit my code

script.js:

function displayuser() {
  $("#mySecond").empty();
    var x = document.getElementById("mySelect").value;

  $.ajax({
      url: "https://cubber.zendesk.com/api/v2/organizations/"+x+"/users.json",
      type: 'GET',
      dataType: 'json',
      cors: true ,
            contentType:'application/json',
            secure: true,
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("claire.pagniez@cubber.com:CC..11cc"));
            },
            success: function (data){
                for (i = 0; i < data.users.length; i++) {

          var username = data.users;
                          data.users.sort(function (a, b) {
                         return a.name.localeCompare(b.name);
                     });

                var userid = data.users[i].id;
        var all = data.users[i];
        console.log(all);

            $("#mySecond").append('<option value="'+ userid +'">'+ username +'</option>')

                }
            },
  });
}

enter image description here

xenurs
  • 459
  • 1
  • 8
  • 19
  • 4
    "not working" isn't a particularly useful diagnosis of the problem – James Thorpe Mar 31 '16 at 11:39
  • script.js:21 Uncaught TypeError: userarray.sort is not a function the error is saying – xenurs Mar 31 '16 at 11:40
  • 1
    that's not how Array.prototype.sort work. Try this http://stackoverflow.com/questions/6712034/sort-array-by-firstname-alphabetically-in-javascript – thangngoc89 Mar 31 '16 at 11:40
  • we'd need to see the actual structure of the `data`, though it does seem you sort `data.users[i].name` which appears to be the name (again, if this is an array, show us the data) – Rogier Spieker Mar 31 '16 at 11:41
  • 1
    I don't think that userarray is an array. You reassign the variable on each iteration with only one value. – VeganCreamPie Mar 31 '16 at 11:41
  • Is `data.users[i].name` an array? From just the name, it looks like it might be just a string, which isn't something you can sort by itself. – senschen Mar 31 '16 at 11:41
  • i think usearray in your code is a string not array , – Shushanth Pallegar Mar 31 '16 at 11:42
  • you maybe want to do something like `data.users.sort(function(a, b) {return a.name < b.name ? -1 : +(a.name > b.name);})` where both `a` and `b` will be user records (e.g. compare `a.name` to `b.name`) – Rogier Spieker Mar 31 '16 at 11:43

2 Answers2

3

You are trying to sort a string, you actually want to sort data.users, so you don't need to loop. Using sort combined with String.prototype.localCompare you can achieve what you want:

        success: function (data){
            var users = data.users;
            users.sort(function (a, b) {
                return a.name.localeCompare(b.name);
            });

           // users is now sorted
        }
axelduch
  • 10,769
  • 2
  • 31
  • 50
  • #axelduch If i do that i have a list of "object" see my screenshot i added to my post – – xenurs Mar 31 '16 at 12:08
  • To me this seems to be another question, your initial question was answered. I suggest you create another one with the problem that came after the error disappeared. – axelduch Mar 31 '16 at 12:10
1

how can i sort by alphabetical order my list of username

Assuming this is the username array data.users, you can sort it by name

data.users.sort(function(a,b){ return a.name.localeCompare(b.name); });

and then render them

 for (i = 0; i < data.users.length; i++) 
 {
     var user = data.users[i];
     $("#mySecond").append('<option value="'+ user.id +'">'+ user .name +'</option>')
 }
gurvinder372
  • 66,980
  • 10
  • 72
  • 94