-2

I have a a webpage of arrays that I need to convert into readable data. How do I sort the arrays with jquery/Ajax?

Here is the link https://fcctop100.herokuapp.com/api/fccusers/top/recent

Rocket
  • 13
  • 7
  • Possible duplicate of http://stackoverflow.com/questions/28911984/filtering-objects-from-external-array-to-create-a-list-using-jquery-ajax-and – JGV May 16 '16 at 21:41
  • You send the request, receive the data and then process the information using map or reduce or an algorithm... that depends on what you need to do with it, what you want to accomplish... – DIEGO CARRASCAL May 16 '16 at 21:45

2 Answers2

0

you can sort an array in javascript by passing in a function to the sort method. In the function you return either -1 or 1 depending on the comparison of 2 elements. Below example relies on an Order attribute, modify to your need. aoSortedUrgencies is an array of objects.

aoSortedUrgencies.sort(function (a, b) { return ((a.Order< b.Order) ? -1 : (a.Order> b.Order? 1 : 0)) });

After the sort you can then call this to create a JSON string to pass into the ajax call:

JSON.stringify(aoSortedUrgencies)
aggaton
  • 3,066
  • 2
  • 25
  • 36
-1

Depends on what do you want to sort them by.

Lets say you want to sort them by username, you can use this answer to solve your problem.

In the example below I've used the said function to sort your data by username.

//This will sort your array
//srot function from https://stackoverflow.com/a/5503957/3591300
function SortByName(a, b){
  var aName = a.username.toLowerCase();
  var bName = b.username.toLowerCase(); 
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}
$.get("https://fcctop100.herokuapp.com/api/fccusers/top/recent", function(data) {

//here we run the function to sort the array of data before transforming it to table
data.sort(SortByName);
  var table = '<table>'
  for (i = 0; i < data.length; i++) {
    table += '<tr><td>' + data[i].alltime + '</td><td><img width=20 height=20 src="' + data[i].img + '"></td><td>' + data[i].lastUpdate + '</td><td>' + data[i].recent + '</td><td>' + data[i].username + '</td></tr>';

  }
  $('body').append(table);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
Community
  • 1
  • 1
Amin Gharavi
  • 452
  • 3
  • 14