Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

More Useful Array Methods

In JavaScript, we have a couple of more methods to sort the array, find min/max in the array, reverse the array, etc. These are very much useful while solving complex programming questions. Let us see them one by one:

sort():

Example:
var stud = ["Adarsh", "Utkarsh", "Harsh", "Shivam"];
stud.sort();
Now stud will become: ["Adarsh", "Harsh", "Shivam", "Utkarsh"]

reverse():

Example:
var stud = ["Adarsh", "Utkarsh", "Harsh", "Shivam"];
stud.reverse();
Now stud will become: ["Shivam", "Harsh", "Utkarsh", "Adarsh"]

In JavaScript, if we use the sort() function, it will sort values as strings. That means 68 will be kept after 1222 as it is lexicographically greater than 1222 but mathematically it should present before 1222. So we can’t use the sort() function directly for sorting numbers. We may solve this issue using compare function.

Compare Function

Using compare function we may define our own sorting order over the elements. The compare function should return a negative, zero, or positive value, depending on the arguments.

Let us take an example to understand it. If we sort the array of numbers then it will not be sorted mathematically but lexicographically. Suppose we want to make a compare function to sort them in increasing order mathematically.

The required function will be: function(a, b){return a - b} and to sort using it we will write array.sort(function(a, b){return a - b}).

The compare function compares all the values in the array, two values at a time (a, b) and

  • If the result is negative a is sorted before b
  • If the result is positive b is sorted before a
  • If the result is 0 no changes are done with the sort order of the two values.
Example:
var A = [4, 20, 10, 50];
A.sort(function(a, b){return a-b});
Now A will be sorted and will become: [4, 10, 20, 50]

Finding max of an array:

We may use Math.max.apply to find the highest number in an array.

Example:
var A = [4, 50, 1, 20];
var z = Math.max.apply(null,A);
Here 50 will be stored in z.

Finding min of an array:

We may use Math.min.apply to find the lowest number in an array.

Example:
var A = [4, 20, 1, 50];
var z = Math.min.apply(null,A);
Here 1 will be stored in z.

Try the following example in the editor below.

You are given an array of integers with its size. Sort them and print them. Please note that you have to print one element in one line.

Sample Input:

4(Size)
10
2
30
14

Sample Output:

2
10
14
30
Start solving More Useful Array Methods on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test