Q:

How to use MatSort with table of type Object[]

I wanted to use SortHeader in table of type Object, I don't want to change the type of my table to MatTableDataSource, Any Help ?
This is my datasource
elements: Object[] = []

what should I do in my ngOnInit() ?
this doesn't work
this.elements.sort = this.sort;

A:

You need to listen to the matSortChange event on your table.
<table #myTable mat-table [dataSource]="sortedData" matSort (matSortChange)="sortData($event)">

Implement custom sorting logic and provide the sorted data when matSortChange triggers.
@ViewChild('myTable') table: MatTable<Object>;

data: Object[] = [
    { name: 'Frozen yogurt', calories: 159, fat: 6, carbs: 24, protein: 4 },
    { name: 'Ice cream sandwich', calories: 237, fat: 9, carbs: 37, protein: 4 },
    { name: 'Eclair', calories: 262, fat: 16, carbs: 24, protein: 6 },
    { name: 'Cupcake', calories: 305, fat: 4, carbs: 67, protein: 4 },
    { name: 'Gingerbread', calories: 356, fat: 16, carbs: 49, protein: 4 },
];

sortedData: Object[];

constructor() {
    this.sortedData = this.data.slice();
}

sortData(sort: Sort) {
    const data = this.data.slice();
    if (!sort.active || sort.direction === '') {
      this.sortedData = data;
      return;
    }

    this.sortedData = data.sort((a, b) => {
      const isAsc = sort.direction === 'asc';
      switch (sort.active) {
        case 'name': return compare(a['name'], b['name'], isAsc);
        case 'calories': return compare(a['calories'], b['calories'], isAsc);
        case 'fat': return compare(a['fat'], b['fat'], isAsc);
        case 'carbs': return compare(a['carbs'], b['carbs'], isAsc);
        case 'protein': return compare(a['protein'], b['protein'], isAsc);
        default: return 0;
      }
    });
}

function compare(a: number | string, b: number | string, isAsc: boolean) {
    return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

If you don't supply a new sorted array as dataSource on sort events but instead do the sort in place (in the existing dataSource array), you additionally have to call this.table.renderRows() after the sort as the table will not automatically check the array for changes.
https://stackblitz.com/edit/angular-dwkods

