10 examples of 'javascript sort 2d array' in JavaScript

Every line of 'javascript sort 2d array' 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
716function sort(array) {
717 return array.sort(sortFn);
718}
229function sort(array) { return array.sort(sortFn); }
20export function sort(array: T[]): T[] {
21 return array.sort((a, b) => (a.sortId === undefined || b.sortId === undefined) ? 0 : a.sortId - b.sortId);
22}
50function sort(array, less) {
51 var result = [];
52 var currentPositions = [];
53 var current;
54 var position;
55 for (var i = 0; i < array.length; i += 1) {
56 current = array[i];
57 position = less[current];
58 if (currentPositions[current] === undefined) {
59 currentPositions[current] = position;
60 }
61 result[currentPositions[current]] = current;
62 currentPositions[current] += 1;
63 }
64 return result;
65}
1export function sort(arr) {
2 return [...arr].sort((a, b) => a - b);
3}
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}
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=0 && tmp < result[j]){
985 result[j+1] = result[j];
986 j--;
987 }
988 result[j+1] = tmp;
989 }
990 return result;
991}
8function sort(object) {
9 if (sortArrays === true && Array.isArray(object)) {
10 return object.sort();
11 }
12 else if (typeof object !== "object" || object === null) {
13 return object;
14 }
15
16 return Object.keys(object).sort().map(function (key) {
17 return {
18 key: key,
19 value: sort(object[key])
20 };
21 });
22}
7function sort(object) {
8 if (sortArrays === true && Array.isArray(object)) {
9 return object.sort();
10 }
11 else if (typeof object !== "object" || object === null) {
12 return object;
13 }
14
15 return Object.keys(object).sort().map(function(key) {
16 return {
17 key: key,
18 value: sort(object[key])
19 };
20 });
21}
54function sortObjectArray(arr: T[]) {
55 return arr.sort(compareObjects);
56}

Related snippets