10 examples of 'mongoose sort' in JavaScript

Every line of 'mongoose 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

1export function sort(arr) {
2 return [...arr].sort((a, b) => a - b);
3}
60function sort (obj, query, sub = "") {
61 const queries = explode(query.replace(/\s*asc/ig, "").replace(/\s*desc/ig, " desc")).map(i => i.split(" ")),
62 sorts = [];
63
64 if (sub) {
65 sub = "." + sub;
66 }
67
68 each(queries, i => {
69 const desc = i[1] === "desc",
70 y = desc ? 1 : -1,
71 x = desc ? -1 : 1;
72
73 let s = ".",
74 e = "";
75
76 if (notDot.test(i[0])) {
77 s = braceS;
78 e = braceE;
79 }
80
81 sorts.push("if (a" + sub + s + i[0] + e + " !== undefined && b" + sub + s + i[0] + e + " !== undefined) {");
82 sorts.push(" if (a" + sub + s + i[0] + e + " < b" + sub + s + i[0] + e + ") return " + y + ";");
83 sorts.push(" if (a" + sub + s + i[0] + e + " > b" + sub + s + i[0] + e + ") return " + x + ";");
84 sorts.push("} else {");
85 sorts.push(" if (a" + sub + s + i[0] + e + " !== undefined) return " + y + ";");
86 sorts.push(" if (b" + sub + s + i[0] + e + " !== undefined) return " + x + ";");
87 sorts.push("}");
88 });
89
90 sorts.push("return 0;");
91
92 return obj.sort(new Function("a", "b", sorts.join("\n")));
93}
18function sort(object, collection, options){
19 options = options || {}
20
21 options.type = options.type || 'after'
22 options.key = options.key || options.type
23 options.compareKey = options.compareKey || 'id'
24 if (options.index == null){
25 options.index = collection.indexOf(object)
26 }
27
28 if (object[options.key] && !inCorrectLocation(object, collection, options)){
29
30 var correctIndex = getCorrectIndex(object, collection, options)
31 if (correctIndex != options.index && correctIndex != -1){
32 if (!options.shouldAvoidDuplicates || !isConflict(object, collection[correctIndex], options.key)){
33 // append ...
34 if (correctIndex === collection.length){
35 collection.splice(options.index, 1)
36 collection.push(object)
37
38 // or insert
39 } else {
40 collection.splice(options.index, 1)
41 if (correctIndex > options.index){
42 correctIndex -= 1 // shift up if needed
43 }
44 collection.splice(correctIndex, 0, object)
45 }
46
47 return getChange(collection, correctIndex, options)
48
49 }
50 }
51
52 }
53
54}
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}
716function sort(array) {
717 return array.sort(sortFn);
718}
229function sort(array) { return array.sort(sortFn); }
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}
126sort(field, value = 'desc') {
127 if (!Array.isArray(field) && typeof field !== 'object' && typeof field !== 'string') {
128 throw new TypeError(`Expected \`field\` to be array, object or string, got ${typeof field}`);
129 }
130
131 if (typeof value !== 'string' && typeof value !== 'number') {
132 throw new TypeError(`Expected \`value\` to be string or number, got ${typeof value}`);
133 }
134
135 if (Array.isArray(field)) {
136 field.forEach(field => this.sort(field));
137 return this;
138 }
139
140 const sort = isObject(field) ? field : {[field]: value};
141 this.query.push(['sort', sort]);
142
143 return this;
144}

Related snippets