-1

this code is fetching data of Covid19 statistics using API currently its showing data of covid cases of all countries in Alphabetical order ......i want to show data in descending order i.e country with more cases should come first in table

$.ajax({
  url: "https://api.covid19api.com/summary",
  type: "GET",
  dataType: 'JSON',
  success: function(data) {
    console.log(data);
    console.log(data.Countries);
    var sno = 1;
    $.each(data.Countries, function(key, value) {

      $("#country-wise").append("<tr>" +
        "<td>" + sno + "</td>" +
        "<td>" + value.Country + "</td>" +
        "<td>" + value.NewConfirmed + "</td>" +
        "<td>" + value.NewDeaths + "</td>" +
        "<td>" + value.NewRecovered + "</td>" +
        "<td>" + value.TotalConfirmed + "</td>" +
        "<td>" + value.TotalDeaths + "</td>" +
        "<td>" + value.TotalRecovered + "</td>" +
        "</tr>");
      sno++;
    });
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="country-wise"></table>
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 2
    [How do I create a runnable stack snippet?](https://meta.stackoverflow.com/questions/358992) – adiga May 21 '20 at 11:16
  • Countries: Array(186) [0 … 99] 0: Country: "Afghanistan" CountryCode: "AF" Date: "2020-05-21T10:46:21Z" NewConfirmed: 492 NewDeaths: 9 NewRecovered: 80 Slug: "afghanistan" TotalConfirmed: 8145 TotalDeaths: 187 TotalRecovered: 930 – ABDUL BASIT May 21 '20 at 11:17
  • this is for 1 country – ABDUL BASIT May 21 '20 at 11:17
  • Welcome to StackOverflow, the [`sort` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) of an array - can receive a sort function, just use it to sort whatever field you want - BTW, this is a great project to try VueJs – balexandre May 21 '20 at 11:17
  • `data.Countries.sort(a => b.TotalConfirmed - a.TotalConfirmed )` – adiga May 21 '20 at 11:18
  • @ABDULBASIT give VueJs a try... https://codepen.io/balexandre/pen/vYNbKJr?editors=1010 (if you get HTTP 429, wait a bit and try again, everyone's using `localhost` to test) ‍♂️ – balexandre May 21 '20 at 11:51

1 Answers1

0

Use the sort method of Array.

$.ajax({
  url: "https://api.covid19api.com/summary",
  type: "GET",
  dataType: 'JSON',
  success: function(data) {
    console.log(data);
    console.log(data.Countries);
    data.Countries.sort((a, b) => b.TotalConfirmed - a.TotalConfirmed);
    var sno = 1;
    $.each(data.Countries, function(key, value) {
      console.log(key + ":" + value);

      $("#country-wise").append("<tr>" +
        "<td>" + sno + "</td>" +
        "<td>" + value.Country + "</td>" +
        "<td>" + value.NewConfirmed + "</td>" +
        "<td>" + value.NewDeaths + "</td>" +
        "<td>" + value.NewRecovered + "</td>" +
        "<td>" + value.TotalConfirmed + "</td>" +
        "<td>" + value.TotalDeaths + "</td>" +
        "<td>" + value.TotalRecovered + "</td>" +
        "</tr>");
      sno++;
    });
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="country-wise"></table>
Ehsan Mahmud
  • 725
  • 4
  • 11
  • great it worked ....i was stuck for hours...thanx alot.. now kindly help me to add option of search by country in this table. – ABDUL BASIT May 21 '20 at 11:24
  • You have to attempt it and come back if you do not know how to proceed. But I can give you a hint. `filter` function – Ehsan Mahmud May 21 '20 at 11:25