I have a problem with the sort() method, and i cant figure it out.
So basically I have a table with crypto currencies from an api, and it can sort per column. everything works fine (Rank, Name, MarketCap, etc) except the price of the coin. This just doesn't sort correctly. for example the price of BTC always ends up somewhere in the middle. The price from the api is a String.
This is the method I use for sorting.
sortBy (prop) {
if (this.sorter === 'inc') {
this.cryptos.sort((a, b) => a[prop] > b[prop] ? 1 : -1)
this.sorter = 'dec'
} else {
this.cryptos.sort((a, b) => a[prop] > b[prop] ? -1 : 1)
this.sorter = 'inc'
}
},
And this is the way I call the method
<tr>
<th class="table-header table-link" v-on:click="sortBy('rank')">Rank</th>
<th class="table-header">Icon</th>
<th class="table-header table-link" v-on:click="sortBy('name')">Name</th>
<th class="table-header">Symbol</th>
<th class="table-header table-link" v-on:click="sortBy('price')">Price</th>
<th class="table-header table-link" v-on:click="sortBy('change')">{{time}}</th>
<th class="table-header table-link" v-on:click="sortBy('marketCap')">Market Cap</th>
</tr>
Example of amounts that are coming through (keep in mind that they are coming through as a String):
464.2807003746
15150.0527417405
0.5010685149
0.8374992554
0.5010685149
66.5544823869
233.8180345945
4.2255392708
10.9344985436
0.1363374574
0.16150966
I hope I gave enough info, and maybe someone can help me with this. Thank you in advance!
parseFloat(prop)in your sortBy function, but only for float or int values (not for name).