title: Sorting results from SharePoint RestAPI
body: <p>I'm trying to build some semi-dynamic staff directory pages from a Sharepoint List, using their REST API.</p>
  
  <p>I know enough Javascript to get me in trouble, so I have a feeling my solution is not as elegant as it could be and probably not ideal.</p>
  
  <p>The goal is to allow our web editors to create unique pages for various departments by simply adding a short script and the employee's email address. Another option I've thought about is simply adding a field in the list for each program but it's not consistent enough across the board, so being able to set the list and sort on each page is probably the best option.</p>
  
  <p>The solution works just fine but occasionally the sort order gets out of wack, I'm guessing because of the order the script reads the list.</p>
  
  <p>Here's the front end code:</p>
  
  <pre><code>&lt;script type="text/javascript" 
  src="getstaffdirectory2.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  $(document).ready(function() {
  GetStaffDirectory('email1@dcccd.edu');
  GetStaffDirectory('email2@dcccd.edu');
  GetStaffDirectory('email3@dcccd.edu');
  GetStaffDirectory('email4@dcccd.edu');
  GetStaffDirectory('email5@dcccd.edu'); 
  });
  &lt;/script&gt;
  
  &lt;div class="white-space-2"&gt;&lt;/div&gt;
  &lt;div class="row"&gt;
  &lt;div id="GetStaffDirectory"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Rather un-elegant? :-)</p>
  
  <p>Here's the script:</p>
  
  <pre><code>function GetStaffDirectory(UserEmail) {
  var res;
  var selectStr = '$select=';
  var expandStr = '$expand=';
  var filterStr = '$filter=(EMAIL eq \''+UserEmail+'\')';
  
  var requestURL = '/_api/web/lists/GetByTitle(\'Staff 
  Directory\')/items?'+filterStr; // + '?' + selectStr + '&amp;' + expandStr + '&amp;' 
  + filterStr;
  console.log(requestURL);
  $.ajax({
      url: requestURL,
      headers: {
          Accept: 'application/json;odata=verbose'
      },
      method: 'GET',
      //Success Function
      success: function (data) {
          res = data.d.results;
          console.log(res);
          // College Name Function
          switch(res[0].LOC){
              case "BHC" : {
                              loc1="Brookhaven College";
                              break;
                           }
              case "CVC" : {
                              loc1="Cedar Valley College";
                              break;
                           }
              case "EFC" : {
                              loc1="Eastfield College";
                              break;
                           }
              case "ECC" : {
                               loc1="El Centro College";
                              break;
                           }
              case "MVC" : {
                              loc1="Mountain View College";
                              break;
                           }
              case "NLC" : {
                              loc1="North Lake College";
                              break;
                           }
              case "RLC" : {
                              loc1="Richland College";
                              break;
                           }
              case "DCO" : {
                              loc1="Lecroy Center/ Dallas Colleges Online";
                              break;
                           }
              case "DSC" : {
                              loc1="District Service Center";
                              break;
                           }
              case "DO" :  {
                              loc1="District Office";
                              break;
                           }
              default : {
                  loc1="";
                  break;
              }
          }
          if((res[0].Office_x0020_Number == null)||(res[0].Office_x0020_Number 
  == undefined)){
               officeNum = "";
           }
          else {
           officeNum = '&lt;li class="contact-office"&gt;' + 
  res[0].Office_x0020_Number + '&lt;/li&gt;\n'
           }
           if((res[0].biolink == null)||(res[0].biolink == undefined)){
                biolinkURL = "";
            }
           else {
            biolinkURL = '&lt;li class="contact-bio"&gt;&lt;a href="' + res[0].biolink + '"&gt;Online Bio&lt;span class="sr-only"&gt; for ' + res[0].Title + '&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;\n'
            }
            if((res[0].staff_x002d_photo == null)||(res[0].staff_x002d_photo 
  == undefined)){
                 staffphoto = '/logo-' + 
  res[0].LOC + '-square.svg'
             }
             else {
              staffphoto = res[0].staff_x002d_photo;
            }
  
          var contactString =
          '&lt;div class="col-sm-4"&gt;\n' +
          '&lt;div class="contact-box height-sm-400 ' + res[0].LOC + '"&gt;\n' +
          '&lt;img class="contact-photo" src="' + staffphoto + '"&gt;\n' +
          '&lt;div class="contact-name"&gt;' + res[0].FIRST + ' ' + res[0].Title + 
  '&lt;/div&gt;\n' +
          '&lt;div class="contact-title"&gt;' + res[0].Title1 + '&lt;/div&gt;\n' +
          '&lt;div class="contact-dept"&gt;' + res[0].department + '&lt;/div&gt;\n' +
          '&lt;ul class="contact-info"&gt;\n' +
          '&lt;li class="contact-location"&gt;' + loc1 + '&lt;/li&gt;\n' +
          officeNum +
          '&lt;li class="contact-email"&gt;&lt;a href="mailto:' + res[0].EMAIL + '"&gt;' + 
  res[0].EMAIL + '&lt;/a&gt;&lt;/li&gt;\n' +
          '&lt;li class="contact-phone"&gt;' + res[0].PHONE + '&lt;/li&gt;\n' +
          biolinkURL +
          '&lt;/ul&gt;\n&lt;/div&gt;\n&lt;/div&gt;\n&lt;/div&gt;\n';
          $("#GetStaffDirectory").append(contactString);
      },
      //Error Function
      error: function (jQxhr, errorCode, errorThrown) {
          res = jQxhr;
          console.log(res);
          $("#AjaxLoader0").hide();
          $(".AjaxMessage").html(errorThrown);
      },
      dataType: 'json' //Make me a JSON
  });
  };
  </code></pre>
  
  <p>Is there an easier way to ensure the script pulls the data in the order listed on the page? I'm guessing there's some method with an array but I'm not as familiar with how they work.</p>