10 examples of 'datatable default sort' in JavaScript

Every line of 'datatable default sort' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
291sort: function sort(order, col, grid) {
292 var compare = order === 'desc' ? function (v1, v2) {
293 return v1 === v2 ? 0 : v1 > v2 ? 1 : -1;
294 } : function (v1, v2) {
295 return v1 === v2 ? 0 : v1 < v2 ? 1 : -1;
296 };
297 records.sort(function (r1, r2) {
298 return compare(r1.no, r2.no);
299 });
300 grid.records = records;
301},
90function quickSort(left, right) {
91 if (left < right) {
92 // QuickSort's effectiveness depends on its ability to partition the
93 // sequence being sorted into two subsequences of roughly equal length:
94 // by halving the length at each recursion level, the subproblems get
95 // smaller faster. To get a good partition, we must choose a pivot value
96 // that is close to the median of the values in the sequence: by
97 // definition, half of the values will fall before the median, half
98 // after it.
99 // If the sequence is already mostly sorted, then choosing the first or
100 // last value as the pivot is probably as far from the median as you
101 // could possibly get; this is a "pessimal", not optimal, choice.
102 // Choosing the middle value as the pivot is likely to be much closer to
103 // the median.
104 const pivot = (left + right) >> 1;
105 const partitionIndex = partition(pivot, left, right);
106
107 // Sort left and right
108 quickSort(left, partitionIndex - 1);
109 quickSort(partitionIndex + 1, right);
110 }
111}
128setSorting(sorting: DataSorting): void {
129 this._sorting = sorting;
130
131 if (sorting && sorting.key) {
132 this._rows.sort((a: DataRow, b: DataRow) => {
133 let left = a.getValue(sorting.key);
134 if (left) {
135 left = (left instanceof Date) ? left.valueOf().toString() : left.toString();
136 } else {
137 left = '';
138 }
139
140 let right = b.getValue(sorting.key);
141 if (right) {
142 right = (right instanceof Date) ? right.valueOf().toString() : right.toString();
143 } else {
144 right = '';
145 }
146
147 return sorting.direction === 'asc'
148 ? left.localeCompare(right)
149 : right.localeCompare(left);
150 });
151 }
152}
165private changeSort(data: any): any {
166 const columns = this.sortedColumns || [];
167 const length: number = columns.length;
168
169 if (!this.sorting || (length === 0)) {
170 return data;
171 }
172
173 let columnSorted: MagicTableColumn;
174 const alphaSort = function (a: any, b: any, sort: any) {
175 if (typeof a === 'string') {
176 a = a.toLowerCase();
177 b = b.toLowerCase();
178 }
179
180 if (a > b) {
181 return sort === 'desc' ? -1 : 1;
182 } else if (a < b) {
183 return sort === 'asc' ? -1 : 1;
184 }
185
186 return 0;
187 };
188
189 return data.sort(function (a: any, b: any) {
190 let tem = 0, index = 0;
191 while (tem === 0 && index < length) {
192 columnSorted = columns[index];
193 tem = alphaSort(a[columnSorted.field], b[columnSorted.field], columnSorted.sort);
194 index += 1;
195 }
196 return tem;
197 });
198}
41sortData(data: any[], sortBy: string, sortOrder: TdDataTableSortingOrder = TdDataTableSortingOrder.Ascending): any[] {
42 if (sortBy) {
43 data = Array.from(data); // Change the array reference to trigger OnPush and not mutate original array
44 data.sort((a: any, b: any) => {
45 const compA: any = a[sortBy];
46 const compB: any = b[sortBy];
47 let direction: number = 0;
48 if (!Number.isNaN(Number.parseFloat(compA)) && !Number.isNaN(Number.parseFloat(compB))) {
49 direction = Number.parseFloat(compA) - Number.parseFloat(compB);
50 } else {
51 if (compA < compB) {
52 direction = -1;
53 } else if (compA > compB) {
54 direction = 1;
55 }
56 }
57 return direction * (sortOrder === TdDataTableSortingOrder.Descending ? -1 : 1);
58 });
59 }
60 return data;
61}
170sort(key?: string, direction?: string): void {
171 let sorting = this._sorting || new DataSorting();
172 if (key) {
173 sorting.key = key;
174 sorting.direction = direction || 'asc';
175 }
176 this.setSorting(sorting);
177}
363private sortRows(rows: Row[]): Row[] {
364 if (!this.sortingSettings.length) {
365 return rows;
366 }
367
368 let sortings = this.sortingSettings.slice();
369 return rows.slice().sort((a, b) => {
370 let res;
371 for (let i = 0, len = sortings.length; i < len; i++) {
372 let s = sortings[i];
373 let accessor = functor(s.accessor);
374 res = s.comparer(accessor(a), accessor(b));
375 if (res !== 0) return res;
376 }
377 return res;
378 });
379}
190public set defaultSortOrder(newValue: SortOrder) {
191 this._defaultSortOrder = newValue;
192 this.updateSortOrderClass(newValue);
193};
132public get isSorting() { return this.header && this.header.colSorting; }
167calcSortDir(sorts: any[]): any {
168 if (sorts && this.column) {
169 const sort = sorts.find((s: any) => {
170 return s.prop === this.column.prop;
171 });
172
173 if (sort) return sort.dir;
174 }
175}

Related snippets