I have a json object list (carriers) like this:
In my *.vue I render this with:
<tr v-for="carrier in this.carriers">
<td>{{ carrier.id }}</td> ....
My thead / th's can be clicked for sorting the table like this:
<thead>
<tr>
<th class="pointer link" @click="sort('id')">
ID
<span v-if="'id' === currentSortCol">
{{currentSortDir ==='asc' ? '↑':'↓'}}
</span>
</th>
<th class="pointer link" @click="sort('region.name')">
Region
<span v-if="'region.name' === currentSortCol">
{{currentSortDir ==='asc' ? '↑':'↓'}}
</span>
</th>....
and my sort method is like this:
sort(col) {
if (this.currentSortCol === col) {
this.currentSortDir = this.currentSortDir === "asc" ? "desc" : "asc";
} else {
this.currentSortCol = col;
}
this.carriers.sort(this.sortBy(col, this.currentSortDir));
},
sortBy(property, order) {
this.currnetSortDir=order;
return function(a, b) {
const varA =
typeof a[property] === "string"
? a[property].toUpperCase()
: a[property];
const varB =
typeof b[property] === "string"
? b[property].toUpperCase()
: b[property];
let comparison = 0;
if (varA > varB) comparison = 1;
else if (varA < varB) comparison = -1;
return order === "desc" ? comparison * -1 : comparison;
};
}
Problem: The sorting asc, desc is fine with the ID (carrier.id) . But it will not sort the nesting carrier.region.name column. How can I sort a nesting column, like this carrier.region.name?
