1π
Use _.sortBy()
with a callback, and convert the total
to a number:
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, v => Number(v.total));
console.log(JSON.stringify(ascending));
}
function test2() {
descending = ascending.reverse();
console.log(JSON.stringify(descending));
}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>
π€Ori Drori
- [Vuejs]-Uncaught ReferenceError: Vue is not defined
- [Vuejs]-Custom font is not showing on Electron + Vue App
1π
You can do it like this
Here in sort
for comparison we have to change string to number.
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 = myArray.sort((a,b)=>Number(a.total)-Number(b.total));
console.log(JSON.stringify(ascending));
}
function test2() {
descending = ascending.reverse();
console.log(JSON.stringify(descending));
}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>
π€Code Maniac
1π
Use map to create a new array where the value of total
will be a number. Then use sort function.
The below function sorts the array in ascending order
var myArray = [{
name: 'David',
total: "6"
}, {
name: 'John',
total: "2"
}, {
name: 'Joe',
total: "8"
}, {
name: 'Ana',
total: "14"
}];
let sortedOrder = myArray.map((item) => {
return {
name: item.name,
total: parseInt(item.total, 10)
}
}).sort((a, b) => {
return a.total - b.total;
})
console.log(sortedOrder)
π€brk
- [Vuejs]-Using v-btn to change route only if some condition is met
- [Vuejs]-VueJs, how to refresh the data that has been fetch through created function
1π
var myArray = [{
name: 'David',
total: "6"
}, {
name: 'John',
total: "2"
}, {
name: 'Joe',
total: "8"
}, {
name: 'Ana',
total: "14"
}];
var ascending;
var descending;
/**
* Ascending
* Calls the sort function of the Array object.
* - Processed the "total" property of each object.
* -- Parsing each string number into a int for comparision.
*/
function test1() {
ascending = myArray.sort(function(a, b) { return parseInt(a['total']) > parseInt(b['total']); });
console.log(JSON.stringify(ascending));
}
/**
* Descending
* Calls the sort function of the Array object.
* - Processed the "total" property of each object.
* -- Parsing each string number into a int for comparision.
*/
function test2() {
descending = myArray.sort(function(a, b) { return parseInt(a['total']) < parseInt(b['total']); });
console.log(JSON.stringify(descending));
}
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>
π€tpayne84
0π
No need for loadlash, just use .sort()
will do the need. take a look at the snippet
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 = myArray.sort(function(a,b){
return Number(a.total) - Number(b.total);
});
console.log(ascending)
}
function test2() {
descending = myArray.sort(function(a,b){
return Number(b.total) - Number(a.total);
});
console.log(descending);
}
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>
π€Akhil Aravind
- [Vuejs]-Due to limitations in JavaScript, Vue cannot detect the changes to an array. What is the limitations?
- [Vuejs]-Export Excel with Laravel, VueJS and Inertiajs
0π
Implement like below for sorting by property βtotalβ β
function getSortOrder(prop) {
return function(a, b) {
if (parseInt(a[prop]) > parseInt(b[prop])) {
return 1;
} else if (parseInt(a[prop]) < parseInt(b[prop])) {
return -1;
}
return 0;
}
}
array.sort(getSortOrder("total"));
π€harshit
- [Vuejs]-Vue3 β How call function from another component
- [Vuejs]-Cannot read property 'text' of undefined β Vue.js
0π
You can use _.orderBy
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 = _.orderBy(myArray, v => parseInt(v.total),['asc']);
console.log(JSON.stringify(ascending));
}
function test2() {
descending = _.orderBy(myArray, v => parseInt(v.total),['desc']);
console.log(JSON.stringify(descending));
}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<button onclick="test1()">Ascending</button>
<button onclick="test2()">Descending</button>
π€Biswadev
- [Vuejs]-Vuejs: How to properly pass props to component data
- [Vuejs]-I want to create a text field in vuetify
Source:stackexchange.com