10 examples of 'mdn array sort' in JavaScript

Every line of 'mdn array 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
this disclaimer
20export function sort<T extends ISort>(array: T[]): T[] {
21 return array.sort((a, b) => (a.sortId === undefined || b.sortId === undefined) ? 0 : a.sortId - b.sortId);
22}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

229function sort(array) { return array.sort(sortFn); }
716function sort(array) {
717 return array.sort(sortFn);
718}
1export function sort(arr) {
2 return [...arr].sort((a, b) => a - b);
3}
83function dsort(a) {
84 return [...a].sort(dSort)
85}
277function sort(arr) {
278 for (var i = 1; i < arr.length; i++) {
279 var tmp = arr[i]
280 var j = i
281 while (arr[j - 1] > tmp) {
282 arr[j] = arr[j - 1]
283 --j
284 }
285 arr[j] = tmp
286 }
287
288 return arr
289}
419export function sort__name(array) {
420 return array.slice(0).sort(_sort__key__array('name'))
421}
34export default function sortBy(array, sortType, order) {
35 let sortedData = [];
36
37 switch (sortType) {
38 case 'attributes.last_released_at':
39 sortedData = array.sort(releasedAtSort);
40 break;
41 case 'newReleases':
42 sortedData = array.sort(releasedAtSort).sort(newReleasesSort);
43 break;
44 case 'attributes.title':
45 sortedData = array.sort(titleSort);
46 break;
47 default:
48 sortedData = array.sort(newReleasesSort);
49 }
50
51 if (order === 'descending') { return sortedData.reverse(); }
52
53 return sortedData;
54}
2export function naturalSort(arr:any[], key:any):any[] {
3 let a, b, a1, b1, rx = /(\d+)|(\D+)/g, rd=/\d+/;
4 return arr.sort((as,bs) => {
5 a = String(eval('as.' + key)).toLowerCase().match(rx);
6 b = String(eval('bs.' + key)).toLowerCase().match(rx);
7 while(a.length && b.length) {
8 a1 = a.shift();
9 b1 = b.shift();
10 if(rd.test(a1) || rd.test(b1)) {
11 if(!rd.test(a1)) return 1;
12 if(!rd.test(b1)) return -1;
13 if(a1! = b1) return a1 - b1;
14 } else if(a1! = b1) {
15 return a1 > b1 ? 1 : -1;
16 }
17 }
18 return a.length - b.length;
19 });
20}
978function sort1(array) {
979 var len = array.length,
980 i, j, tmp, result;
981
982 // 赋予数组副本
983 result = array.slice(0);
984 for(i=1; i<len; i++){
985 tmp = result[i];
986 j = i - 1;
987 while(j>=0 && tmp < result[j]){
988 result[j+1] = result[j];
989 j--;
990 }
991 result[j+1] = tmp;
992 }
993 return result;
994}

Related snippets