0

I am trying to build a single paged website to display covid cases in India. The base url is returning an array sorted alphabetically. I want to sort it according to "totalConfirmed". Here is my code.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Covid19 India</title>
  <!-- Bootstrap CSS 4.3.1 -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

  <!-- Axios -->
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

  <!-- VueJS -->
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  
  <!-- Lodash -->
  <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/0.10.0/lodash.min.js"></script>

</head>
<body>
  <div class="container mt-4" id="app">

    <table class="table-responsive">
    <table class="table">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Location</th>
          <th scope="col">Total</th>
          <th scope="col">Active</th>
          <th scope="col">Recovered</th>
          <th scope="col">Deaths</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="state, index in states">
          <th scope="row">{{ index + 1 }}</th>
          <td>{{ state.loc }}</td>
          <td>{{ state.totalConfirmed }}</td>
          <td>{{ state.totalConfirmed - (state.discharged + state.deaths)}}</td>
          <td>{{ state.discharged }}</td>
          <td>{{ state.deaths }}</td>
        </tr>
      </tbody>
    </table>
    </table>
  </div>
  
  
  <script>
  const cases = [];
    var app = new Vue({
      el: '#app',
      data: {
        states: []
      },
      mounted: function() {
        axios.get('https://api.rootnet.in/covid19-in/stats/latest')
            .then(response => {
              this.states = response.data.data.regional;
              this.states = cases;
              cases = _.orderBy(cases, 'totalConfirmed', 'desc');
              console.log(cases);
            })
            .catch(error => {
              console.log(error);
            });
      }
          
    })
  </script>
</body>
</html>

In my console I am getting an error "TypeError: _.orderBy is not a function". What could it be that I am doing wrong here? If I dont use orderBy my code is running fine with no errors.

Garry
  • 342
  • 5
  • 18
  • Does this answer your question? [use Lodash to sort array of object by value](https://stackoverflow.com/questions/43371092/use-lodash-to-sort-array-of-object-by-value) – Tasos Nov 03 '20 at 12:25

1 Answers1

0

Try adding the lodash library.

const _ = require('lodash');     
cases = _.orderBy(cases, ['totalConfirmed']);
Joseph Cho
  • 4,033
  • 4
  • 26
  • 33
Xigo
  • 29
  • 9
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Nov 03 '20 at 13:31
  • TypeError: _.orderBy is not a function – Garry Nov 03 '20 at 15:08
  • It is a problem with the installation of the package. Try the instructions of this link https://www.npmjs.com/package/lodash.orderby `npm i --save lodash.orderby` and `var orderBy = require('lodash.orderby');` – Henri Nov 03 '20 at 15:45