10 examples of 'array sort mdn' in JavaScript

Every line of 'array sort mdn' 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
229function sort(array) { return array.sort(sortFn); }
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

716function sort(array) {
717 return array.sort(sortFn);
718}
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}
1export function sort(arr) {
2 return [...arr].sort((a, b) => a - b);
3}
83function dsort(a) {
84 return [...a].sort(dSort)
85}
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}
419export function sort__name(array) {
420 return array.slice(0).sort(_sort__key__array('name'))
421}
105var arrsort = arr.sort(function sortNumber(a, b){
106 return a - b;
107});
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}
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}

Related snippets