JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can mixed case array sort is possible

JavaScript Quiz – can mixed case array sort is possible

Mixed Case Array Sort

Array sort method is used to sort the elements of an array in place and return the sorted array. By default sort method, sort array element in ascending order. But for the mixed case, the sort method have different behavior

Array sort method works with alphabets properly as it compares with the first character of a string and so on. But when a mixed case is introduced things change.

Basic Syntax of Array Sort

Array.sort()

For more detail about the Array Sort Method click here.

JavaScript Question

In this javascript quiz, find out can mixed case array sort is possible because array sort works well with similar alphabet cases.

First, we declare an variable mixedCase using let and assign an array to it which contain mixed case alphabet.

let mixedCase = ['C', 'd', 'E', 'b', 'a'];

Now, we use the sort method and check it’s first index value which is at 0.

mixed.sort();
console.log(mixedCase[0]);

In the next step, we user a custom comparing function which compare the value after make element in lower case.

mixedCase.sort(function (a, b) {
    let x = a.toUpperCase(),
        y = b.toUpperCase();
    return x == y ? 0 : x > y ? 1 : -1;

});
console.log(mixedCase[0]);

Any idea what should be the output of code statements.

JavaScript Quiz Test

Here is the full javascript code snippet for you to solve.

let mixedCase = ['C', 'd', 'E', 'b', 'a'];

mixedCase.sort();
console.log(mixedCase[0]); // output => 🧐 ?

mixedCase.sort(function (a, b) {
    let x = a.toUpperCase(),
        y = b.toUpperCase();
    return x == y ? 0 : x > y ? 1 : -1;

});
console.log(mixedCase[0]); // output => 🧐 ?

// Made with ❤️, by @jsstartup

Answer – “C”, “a”

Click To Run Code

Explanation

Now, you know the answer is “C”, “a”. But why doesn’t it give the same output and what does that function is doing. Let me explain

As alphabets can be upper case or lower case but when sort method applied to it then upper case takes priority.

If you check the value of mixedCase after sort then.


let mixedCase = ['C', 'd', 'E', 'b', 'a'];

mixedCase.sort();
console.log(mixedCase); // => ["C", "E", "a", "b", "d"]

Now, after the sort with custom function as element converted to lower for proper sorting.

mixedCase.sort(function (a, b) {
    let x = a.toUpperCase(),
        y = b.toUpperCase();
    return x == y ? 0 : x > y ? 1 : -1;

});
console.log(mixedCase[0]); // => ["a", "b", "C", "d", "E"]

At last, That’s why we get an answer “C”, “a.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – find numbers difference with math abs
  2. JS Quiz – does get date and month from date object is same
  3. JS Quiz – does the string startsWith() method return true
  4. JavaScript Quiz – boolean return value true with string or not

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link