Reputation: 16638
Tech: Angular 7, AngularCli, JS and TS.
In Js how do I sort an array of arrays by asc and desc order
This is my example array:
this.tableRows = [
[ 'zz', 'cc' ],
[ 'aa', 'cc' ],
[ 'uu', 'dd' ]
];
I want to sort the above by column position and asc / desc.
This is my HTML:
<tr ls-table-row class="child-row" *ngFor="let row of tableRows">
<td ls-table-cell *ngFor="let cell of row">{{ cell }}</td>
</tr>
I will be passing in the @inputs of type and columnPosition like so:
@Input() public sortByColumnPosiition = 1;
@Input() public sortByType = 'asc';
These explain the column I want to sort and type explains the direction e.g. desc.
This is my current example for sorting:
sortByColumnPosition = 0;
sortByType = 0;
public sortBy(data: Array<Array<any>>) {
return [ ...data ].sort((a, b) => a[ this.sortByColumnPosition ].localeCompare(b[ this.sortByColumnPosition ]) * this.sortByType);
}
Above is my current attempt - but only works for column 0 and not column 1
Upvotes: 2
Views: 1809
Reputation: 3196
If you are using angular you can use pipes for sorting in asc/desc
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "sort"
})
export class ArraySortPipe implements PipeTransform {
transform(array: any, field: string): any[] {
if (!Array.isArray(array)) {
return;
}
function sortBy(arr, field){
let type = 1;
let col = 0;
if(field == 'DESC'){
type = -1;
col = 0;
}
return [...arr].sort((a,b) => a[col].localeCompare(b[col]) * type)
}
sortBy(array,field)
}
}
<tr ls-table-row class="child-row" *ngFor="let row of tableRows | sort:'ASC'>
<td ls-table-cell *ngFor="let cell of row">{{ cell }}</td>
</tr>
Upvotes: 0
Reputation: 5853
You can use the .localeCompare()
to check which string comes first in the order of sorting.
const tableRows = [
[ 'zz', 'cc' ],
[ 'aa', 'cc' ],
[ 'uu', 'dd' ]
];
function sortAscending(arr) {
// To avoid mutating the original array, make a deep copy
const arrToSort = Array.from(arr);
return arrToSort.sort(([a, b], [c, d]) => a.localeCompare(c) || b.localeCompare(d));
}
function sortDescending(arr) {
// To avoid mutating the original array, make a deep copy
const arrToSort = Array.from(arr);
return arrToSort.sort(([a, b], [c, d]) => c.localeCompare(a) || d.localeCompare(b));
}
const sortedAscArr = sortAscending(tableRows);
const sortedDescArr = sortDescending(tableRows);
console.log(sortedAscArr);
console.log(sortedDescArr);
Upvotes: 0
Reputation: 36584
You can create a function which takes Array, column no and the sort order(asc/desc).
You can use a trick to use a same function for asc/desc sort. Have a variable whose value will be 1
or -1
. Always multiply it with each result or sort()
callback. If you will pass 1
it will sort asc and desc for -1
const tableRows = [
['zz', 'cc'],
['aa', 'cc'],
['uu', 'dd']
];
function sortBy(arr, col, type = 1) {
return [...arr].sort((a, b) => a[col].localeCompare(b[col]) * type)
}
console.log(sortBy(tableRows, 1)) //asc
console.log(sortBy(tableRows, 0, -1)) //desc
Upvotes: 4