-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.js
More file actions
458 lines (362 loc) · 14.1 KB
/
sort.js
File metadata and controls
458 lines (362 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* Array.prototype.sort() Method
*
* Description:
* The sort() method sorts the elements of an array in place and returns the sorted array.
* Default sort order is ascending, built upon converting elements to strings.
*
* Syntax: array.sort([compareFunction])
*
* Parameters:
* - compareFunction (optional): Function that defines the sort order
* - If omitted, array is sorted by converting elements to strings
* - compareFunction(a, b) should return:
* < 0 : a comes before b
* = 0 : a and b order unchanged
* > 0 : b comes before a
*
* Returns: The sorted array (same array, sorted in place)
*
* ⚠️ WARNING: sort() mutates the original array!
*
* Time Complexity: O(n log n) average
* Space Complexity: O(1) or O(n) depending on implementation
*/
// ========================================
// BASIC SORTING
// ========================================
console.log("=== Array.sort() Examples ===\n");
// Example 1: Default string sort
console.log("Example 1: Default string sort");
const fruits = ["banana", "apple", "cherry", "date"];
console.log("Before sort:", fruits);
fruits.sort();
console.log("After sort:", fruits);
console.log("Note: Original array is modified!\n");
// Example 2: Numeric sort problem with default sort
console.log("Example 2: Default sort with numbers (WRONG!)");
const numbers = [1, 2, 10, 21, 3, 100];
console.log("Before sort:", numbers);
const defaultSorted = [...numbers].sort(); // Using spread to avoid mutation
console.log("Default sort (WRONG):", defaultSorted);
console.log("Problem: Numbers converted to strings ('10' < '2')");
console.log();
// Example 3: Correct numeric sort
console.log("Example 3: Correct numeric sort");
const numericSorted = [...numbers].sort((a, b) => a - b);
console.log("Ascending (a - b):", numericSorted);
const descendingSorted = [...numbers].sort((a, b) => b - a);
console.log("Descending (b - a):", descendingSorted);
console.log();
// ========================================
// SORTING OBJECTS
// ========================================
// Example 4: Sorting objects by property
console.log("Example 4: Sorting objects by property");
const users = [
{ name: "John", age: 35 },
{ name: "Jane", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Alice", age: 28 }
];
console.log("Original users:", users);
// Sort by age (ascending)
const byAgeAsc = [...users].sort((a, b) => a.age - b.age);
console.log("Sorted by age (ascending):", byAgeAsc);
// Sort by name (alphabetically)
const byName = [...users].sort((a, b) => a.name.localeCompare(b.name));
console.log("Sorted by name:", byName);
// Sort by age (descending)
const byAgeDesc = [...users].sort((a, b) => b.age - a.age);
console.log("Sorted by age (descending):", byAgeDesc);
console.log();
// Example 5: Multi-level sorting
console.log("Example 5: Multi-level sorting");
const employees = [
{ name: "John", department: "IT", salary: 70000 },
{ name: "Jane", department: "HR", salary: 65000 },
{ name: "Bob", department: "IT", salary: 80000 },
{ name: "Alice", department: "HR", salary: 65000 },
{ name: "Charlie", department: "IT", salary: 70000 }
];
// Sort by department, then by salary
const multiSort = [...employees].sort((a, b) => {
// First, compare departments
const deptCompare = a.department.localeCompare(b.department);
if (deptCompare !== 0) return deptCompare;
// If departments are same, compare salaries
return b.salary - a.salary; // Higher salary first
});
console.log("Sorted by department, then salary (desc):");
multiSort.forEach(emp => {
console.log(` ${emp.department} - ${emp.name}: $${emp.salary}`);
});
console.log();
// ========================================
// ADVANCED SORTING TECHNIQUES
// ========================================
// Example 6: Case-insensitive string sort
console.log("Example 6: Case-insensitive string sort");
const names = ["john", "Alice", "BOB", "charlie", "DAVID"];
console.log("Original:", names);
const caseSensitive = [...names].sort();
console.log("Case-sensitive:", caseSensitive);
const caseInsensitive = [...names].sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase())
);
console.log("Case-insensitive:", caseInsensitive);
console.log();
// Example 7: Sorting with locale
console.log("Example 7: Locale-aware sorting");
const countries = ["Österreich", "Andorra", "Vietnam", "Åland"];
console.log("Original:", countries);
const defaultSort = [...countries].sort();
console.log("Default sort:", defaultSort);
const localeSort = [...countries].sort((a, b) => a.localeCompare(b, 'en'));
console.log("Locale sort (en):", localeSort);
console.log();
// Example 8: Sorting by date
console.log("Example 8: Sorting by date");
const events = [
{ name: "Event A", date: new Date("2024-03-15") },
{ name: "Event B", date: new Date("2024-01-10") },
{ name: "Event C", date: new Date("2024-06-20") },
{ name: "Event D", date: new Date("2024-02-05") }
];
const sortedByDate = [...events].sort((a, b) => a.date - b.date);
console.log("Sorted by date:");
sortedByDate.forEach(event => {
console.log(` ${event.name}: ${event.date.toLocaleDateString()}`);
});
console.log();
// ========================================
// SPECIAL SORTING CASES
// ========================================
// Example 9: Sorting with custom order
console.log("Example 9: Custom priority sorting");
const priorities = { high: 1, medium: 2, low: 3 };
const tasks = [
{ name: "Fix bug", priority: "low" },
{ name: "Deploy app", priority: "high" },
{ name: "Code review", priority: "medium" },
{ name: "Update docs", priority: "low" },
{ name: "Security patch", priority: "high" }
];
const sortedByPriority = [...tasks].sort((a, b) =>
priorities[a.priority] - priorities[b.priority]
);
console.log("Sorted by priority:");
sortedByPriority.forEach(task => {
console.log(` [${task.priority.toUpperCase()}] ${task.name}`);
});
console.log();
// Example 10: Sorting with nulls/undefined
console.log("Example 10: Sorting with null/undefined values");
const mixedArray = [5, null, 2, undefined, 8, null, 1];
console.log("Original:", mixedArray);
const sortedWithNulls = [...mixedArray].sort((a, b) => {
if (a == null) return 1; // Push nulls to end
if (b == null) return -1;
return a - b;
});
console.log("Sorted (nulls at end):", sortedWithNulls);
console.log();
// ========================================
// SORTING ALGORITHMS AND STABILITY
// ========================================
// Example 11: Sort stability
console.log("Example 11: Sort stability");
const students = [
{ name: "John", grade: 85, id: 1 },
{ name: "Jane", grade: 90, id: 2 },
{ name: "Bob", grade: 85, id: 3 },
{ name: "Alice", grade: 90, id: 4 },
{ name: "Charlie", grade: 85, id: 5 }
];
console.log("Original order:");
students.forEach(s => console.log(` ${s.name} (Grade: ${s.grade}, ID: ${s.id})`));
const sortedByGrade = [...students].sort((a, b) => b.grade - a.grade);
console.log("\nSorted by grade (desc):");
sortedByGrade.forEach(s => console.log(` ${s.name} (Grade: ${s.grade}, ID: ${s.id})`));
console.log("Note: Students with same grade maintain original order (stable sort)");
console.log();
// ========================================
// PRACTICAL USE CASES
// ========================================
// Use Case 1: Leaderboard
console.log("Use Case 1: Leaderboard");
const players = [
{ name: "Player1", score: 1500, wins: 10 },
{ name: "Player2", score: 2000, wins: 15 },
{ name: "Player3", score: 1800, wins: 12 },
{ name: "Player4", score: 2000, wins: 14 },
{ name: "Player5", score: 1500, wins: 11 }
];
const leaderboard = [...players].sort((a, b) => {
// Sort by score (desc), then by wins (desc)
if (b.score !== a.score) return b.score - a.score;
return b.wins - a.wins;
});
console.log("Leaderboard:");
leaderboard.forEach((player, index) => {
console.log(` ${index + 1}. ${player.name} - Score: ${player.score}, Wins: ${player.wins}`);
});
console.log();
// Use Case 2: Product listing
console.log("Use Case 2: Product sorting");
const products = [
{ name: "Laptop", price: 999, rating: 4.5, stock: 5 },
{ name: "Mouse", price: 25, rating: 4.8, stock: 50 },
{ name: "Keyboard", price: 75, rating: 4.2, stock: 0 },
{ name: "Monitor", price: 300, rating: 4.7, stock: 10 }
];
// Sort by: in stock first, then by rating, then by price
const sortedProducts = [...products].sort((a, b) => {
// In stock items first
if ((a.stock > 0) !== (b.stock > 0)) {
return a.stock > 0 ? -1 : 1;
}
// Higher rating first
if (Math.abs(b.rating - a.rating) > 0.01) {
return b.rating - a.rating;
}
// Lower price first
return a.price - b.price;
});
console.log("Sorted products (stock > rating > price):");
sortedProducts.forEach(p => {
const stockStr = p.stock > 0 ? `${p.stock} in stock` : 'Out of stock';
console.log(` ${p.name} - $${p.price}, ⭐${p.rating}, ${stockStr}`);
});
console.log();
// Use Case 3: Timeline sorting
console.log("Use Case 3: Timeline/activity feed");
const activities = [
{ user: "John", action: "posted", timestamp: Date.now() - 3600000 },
{ user: "Jane", action: "commented", timestamp: Date.now() - 1800000 },
{ user: "Bob", action: "liked", timestamp: Date.now() - 600000 },
{ user: "Alice", action: "shared", timestamp: Date.now() - 7200000 }
];
const timeline = [...activities].sort((a, b) => b.timestamp - a.timestamp);
console.log("Activity feed (newest first):");
timeline.forEach(activity => {
const timeAgo = Math.floor((Date.now() - activity.timestamp) / 60000);
console.log(` ${activity.user} ${activity.action} (${timeAgo} minutes ago)`);
});
console.log();
// ========================================
// HELPER FUNCTIONS
// ========================================
console.log("=== Sorting Helper Functions ===\n");
// Helper 1: Generic sort by property
function sortByProperty(array, property, descending = false) {
return [...array].sort((a, b) => {
const aVal = a[property];
const bVal = b[property];
if (typeof aVal === 'string') {
return descending
? bVal.localeCompare(aVal)
: aVal.localeCompare(bVal);
}
return descending ? bVal - aVal : aVal - bVal;
});
}
// Helper 2: Multi-property sort
function sortByProperties(array, ...properties) {
return [...array].sort((a, b) => {
for (const prop of properties) {
const { key, descending = false } = typeof prop === 'string'
? { key: prop }
: prop;
const aVal = a[key];
const bVal = b[key];
let comparison = 0;
if (typeof aVal === 'string') {
comparison = aVal.localeCompare(bVal);
} else {
comparison = aVal - bVal;
}
if (comparison !== 0) {
return descending ? -comparison : comparison;
}
}
return 0;
});
}
// Test helpers
console.log("Using sortByProperty helper:");
const testUsers = [
{ name: "Charlie", age: 30 },
{ name: "Alice", age: 25 },
{ name: "Bob", age: 35 }
];
console.log("By name:", sortByProperty(testUsers, 'name'));
console.log("By age (desc):", sortByProperty(testUsers, 'age', true));
console.log();
// ========================================
// PERFORMANCE CONSIDERATIONS
// ========================================
console.log("=== Performance Considerations ===\n");
const largeArray = Array.from({ length: 10000 }, () =>
Math.floor(Math.random() * 1000)
);
console.log("Sorting 10,000 random numbers:");
console.time("Sort performance");
const sorted = [...largeArray].sort((a, b) => a - b);
console.timeEnd("Sort performance");
console.log("First 10 elements:", sorted.slice(0, 10));
console.log();
// ========================================
// CUSTOM IMPLEMENTATION
// ========================================
// Simple bubble sort implementation (for educational purposes)
Array.prototype.bubbleSort = function(compareFunction) {
const arr = [...this];
const compare = compareFunction || ((a, b) => {
if (String(a) < String(b)) return -1;
if (String(a) > String(b)) return 1;
return 0;
});
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (compare(arr[j], arr[j + 1]) > 0) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
};
console.log("Custom bubble sort test:");
const customTest = [64, 34, 25, 12, 22, 11, 90];
console.log("Before:", customTest);
console.log("After bubble sort:", customTest.bubbleSort((a, b) => a - b));
console.log();
// ========================================
// BEST PRACTICES
// ========================================
console.log("=== Best Practices ===");
console.log("1. Always use compare function for numbers: (a, b) => a - b");
console.log("2. Remember: sort() mutates the original array");
console.log("3. Use spread operator [...array] to avoid mutation");
console.log("4. Use localeCompare() for string sorting");
console.log("5. Handle null/undefined values explicitly in compare function");
console.log("6. For multi-level sorting, compare each level in order");
console.log("7. Return 0 in compare function to maintain original order");
console.log("8. Modern JavaScript uses stable sort (as of ES2019)");
console.log("9. Consider performance for large arrays (sort is O(n log n))");
console.log("10. Use locale-aware sorting for international text");
console.log("\n=== Common Patterns ===");
// Pattern 1: Sort and preserve original
const preserve = (arr, compareFn) => [...arr].sort(compareFn);
// Pattern 2: Reverse sort shorthand
const reverseSort = (arr) => [...arr].sort((a, b) => b - a);
// Pattern 3: Random shuffle (Fisher-Yates)
const shuffle = (arr) => {
const shuffled = [...arr];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
return shuffled;
};
console.log("\nShuffled array:", shuffle([1, 2, 3, 4, 5]));