Possible to reverse the ordering?

I have the following webservice call which I am using to populate a dropdown menu in the UI. I am getting these records in increasing number of createDate by the webservice. Is it possible to reverse the order based on the most recent createDate? I mean in decreasing order of dates.

$.ajax({
	    beforeSend: function(request) {
		    request.setRequestHeader("eppn", emailID+"@abc.com");
		  },
	   
	    url: 'http://localhost:8080/Services/getUserByDate?userid=JACK',
	    type : 'GET',
 
	    dataType:'json',
	    success : function(data) {
	            console.log("Sept 18 : Testing getUserByDate webservice");  
	            console.log(data);
	            $.each(data.usersList, function (i) { 
	            $("#userSetList").append('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');
	          	            
	            });
	           
	            
	            
	     },
	    error : function(request,error)
	    {
	    	 $.growl.error({title: "Error", message: "Webservice request failed!" });
             
	    	
	    }
	});

Arrays have a reverse method. You can use data.usersList.reverse() to achieve what you want there.

Thank you. I used it and noticed following:

console.log("Sept 18 : Testing getUserByDate webservice");  
	            console.log(data.usersList.reverse());

Above code showed the items in reversed format as expected. However, when I tested the following:

$.each(data.usersList.reverse(), function (i) { 
	            $("#userSetList").append('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');
	          	            
	            });

It’s not showing items in reverse order. Why?

The array reverse method mutates the array in place.

What that means is that you might need to issue the reverse command first, and then use each.

data.usersList.reverse();
$.each(data.usersList, function (i) { 
    ...
});

Taking a closer look at your HTML code, I’m not sure how an anchor link is supposed to work inside of an option tag. That seems weird and strange to me.

If what you want is to OUTPUT in reverse order, I would suggest:

$.each(data.usersList, function (i) { 
	            $("#userSetList").prepend('<option><a href="#" >'+data.usersList[i].title+' | '+data.usersList[i].userSetId+' | '+moment(data.usersList[i].createDate).format('MM/DD/YYYY')+'</a></option>');
 	            });

Not only should it output the array in reverse order, but it should be more performant, as the script wont need to do an array reverse prior/during the output.

Also, As Paul_W mentioned, if A tags don’t work inside Option tags. If #userSetList is a SELECT tag, and you consider removing the A tag and and adding a value to the option tag. :slight_smile:

hope that helps.

Thanks. That worked.

The reason I have it there is because user will select one thing and inside jquery change function I am storing the user selection into session storage so that I can use it for some webservice call. Please suggest if this isn’t a good option.

Thanks. Would the following thing be workable if I remove the tag?

The reason I have it there is because user will select one thing and inside jquery change function I am storing the user selection into session storage so that I can use it for some webservice call. Please suggest if this isn’t a good option.

Beating added horse here, but another approach would be to se if the API has a sort option (since the service outputs the data from a specific userID, it stands to reason you could pass variables in the query that will tell it to sort by a column in ASC or DESC order)

The reason I have it there is because user will select one thing and inside jquery change function I am storing the user selection into session storage so that I can use it for some webservice call. Please suggest if this isn’t a good option.

just be aware of the following:

  1. option tag will not act as a link from the SELECT menu
  2. the anchor tag wont act as a link OUT of the select menu ( mostly because you have HREF=“#”
  3. the value of the SELECT tag on change will be the text but no tags (as if everything inside the option was run through a strip_tags() function)

Thanks for your input. Unfortunately, API call doesn’t have sort option.

I will try to change this as suggested above and see how it goes. Thanks !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.