Lädt...


🔧 Array.sort() in JavaScript - I was asked about this in an interview


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

I was in an interview and I was given an array of strings.

const arr = [
  "karachi",
  "lahore",
  "kolachi",
  "islamabad"
]

He asked me to sort it in alphabatical order.

I tried:

arr.sort((a, b) => {
  return a < b;
});

He said it will work for numbers (actually it won't) but does not work for strings.

Then I tried

arr.sort((a, b) => {
  return a.charAt(0) < b.charAt(0);
});

He said it will work for just the first characters (actually it won't), then how karachi and kolachi starting with k will be sorted?

I was blank.

He said what are a and b?

I said a is the current element (the element at the index of current iteration) and b is the next element (the element at the current iteration + 1 index).

👉 But actually, it is the opposite. a is the next element and b is the current element.

Then he asked, does the sort modify the original array or returns a new array.

Honestly, most of the time I am working with .map(), .filter(), .some(), and .every(). So I knew the behaviour of these methods but I don't remember when was the last time I used .sort().

I said, it does not modify the original array, rather returns a new array like .map().

👉 But it is the opposite. .sort() modifies the original array and returns the reference to the original array, which is now sorted.

How does actually .sort() work?

Array.sort() accepts a an optional compare function as an argument.

If we do not provide any compare function, the sort method converts all the non-undefined elements to string, and then compare their sequences of UTF-16 code units values.

What does "compare their sequences of UTF-16 code units values" means?
To put it simple, let's say we write character a which is encoded as UTF-16 in JavaScript. In decimal it's value will be 97. For b it will be 98. i.e.
A = 65
B = 66
C = 67
and so on.
I expect you know the ACII table.

So basically the array of strings will automatically be sorted by .sort() method correctly without passing any compare function.

👉 In case of numbers, the behaviour is same;

const arr = [1, 30, 4, 21, 100000];
arr.sort();
console.log(arr);
// expected output: [1, 100000, 21, 30, 4]

Because each number is first converted into string, and then compared with respect to their UTF-16 code units values.

But If we provide a compare function to sort based on numbers:

const arr = [1, 5, 3, 10, 7]
arr.sort((nextValue, prevValue) => {
  // if returnValue > 0, move nextValue after the prevValue
  // if returnValue < 0, move the nextValue before the prevValue
  // if returnValue === 0, keep the original order, do not move any value
  return nextValue - prevValue;
});

So, the sort method can be thought of like this:

function compareFunction(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  a must be equal to b
  return 0;
}

I hope this will make the things a bit clear about Array.sort()

That's it for this post. Write your thoughts in the comments below!

...

🔧 JavaScript Sort Array - How to Sort an Array Accurately


📈 57.82 Punkte
🔧 Programmierung

🔧 Array.sort() in JavaScript - I was asked about this in an interview


📈 55.31 Punkte
🔧 Programmierung

🔧 Bubble Sort, Selection Sort, Insertion Sort | Data Structures & Algorithms in JavaScript


📈 48.96 Punkte
🔧 Programmierung

🔧 Bubble Sort Algorithm - Most Asked Questions About Bubble Sort


📈 42.81 Punkte
🔧 Programmierung

🔧 Insertion Sort Algorithm - Most Asked Questions About Insertion Sort


📈 42.81 Punkte
🔧 Programmierung

🔧 Bubble Sort: Given an Array of Unsorted Items, Return A Sorted Array


📈 37.11 Punkte
🔧 Programmierung

🔧 Array Sort Methods in JavaScript.!


📈 32.21 Punkte
🔧 Programmierung

🔧 Do not use array.sort() Function Without a Compare Function in JavaScript


📈 32.21 Punkte
🔧 Programmierung

🔧 How to Sort an Array of Objects by Property Name in JavaScript


📈 32.21 Punkte
🔧 Programmierung

🔧 Commonly asked ReactJS interview questions. Here are ReactJS interview questions and answers


📈 31.63 Punkte
🔧 Programmierung

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 29.58 Punkte
🔧 Programmierung

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 29.58 Punkte
🔧 Programmierung

🐧 Convert Array of Starings to Array of Numbers in JavaScript


📈 29.58 Punkte
🐧 Linux Tipps

🔧 Learn the JavaScript Array.every() and Array.some() methods


📈 29.58 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 29.58 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 29.58 Punkte
🔧 Programmierung

🔧 JavaScript: Arrays, Array Properties, Array Methods: push, pop, shift, unshift, Stacks, and Queues!


📈 29.58 Punkte
🔧 Programmierung

🔧 Select Element in Array() to a new Array() JavaScript


📈 29.58 Punkte
🔧 Programmierung

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 29.58 Punkte
🔧 Programmierung

🕵️ CVE-2024-6907 | SourceCodester Record Management System 1.0 sort.php sort cross site scripting


📈 28.24 Punkte
🕵️ Sicherheitslücken

🔧 Bucket Sort and Radix Sort


📈 28.24 Punkte
🔧 Programmierung

🐧 Sort a Map in Go by Value (Sort Map by Value)


📈 28.24 Punkte
🐧 Linux Tipps

📰 S3 Ep94: This sort of crypto (graphy), and the other sort of crypto (currency!) [Audio + Text]


📈 28.24 Punkte
📰 IT Security Nachrichten

🔧 Tricky JavaScript Interview Question Using Array And Object Destructuring Combined


📈 26.62 Punkte
🔧 Programmierung

🔧 Tricky JavaScript Interview Question Using Array And Object Destructuring Combined


📈 26.62 Punkte
🔧 Programmierung

⚠️ #0daytoday #Microsoft Windows Array.sort jscript.dll Heap Overflow Exploit [dos #exploits #0day #Exploit]


📈 25.61 Punkte
⚠️ PoC

⚠️ [dos] Microsoft Windows - jscript.dll 'Array.sort' Heap Overflow


📈 25.61 Punkte
⚠️ PoC

💾 Microsoft Windows Array.sort jscript.dll Heap Overflow


📈 25.61 Punkte
💾 IT Security Tools

⚠️ Microsoft Windows Array.sort jscript.dll Heap Overflow


📈 25.61 Punkte
⚠️ PoC

🔧 Array Method Sort on Numbers;


📈 25.61 Punkte
🔧 Programmierung

🔧 912. Sort an Array


📈 25.61 Punkte
🔧 Programmierung

🔧 1636. Sort Array by Increasing Frequency


📈 25.61 Punkte
🔧 Programmierung

🔧 How To Sort Array of Strings


📈 25.61 Punkte
🔧 Programmierung

🔧 1122. Relative Sort Array


📈 25.61 Punkte
🔧 Programmierung

matomo