I am facing a challenge with sorting a JSON object that has a column containing numbers. The issue arises because the column is of string type. How can I sort string numbers in numerical order?
var myArray = [{
name: 'David',
total: "6"
}, {
name: 'John',
total: "2"
}, {
name: 'Joe',
total: "8"
}, {
name: 'Ana',
total: "14"
}];
var ascending;
var descending;
function test1() {
ascending = _.sortBy(myArray, 'total');
console.log(JSON.stringify(ascending));
}
function test2() {
descending = ascending.reverse();
console.log(JSON.stringify(descending));
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701c1f1411031830445e41475e44">[email protected]</a>/lodash.min.js"></script>
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>