What is the parameter passed in Sort()

Asked

Viewed 63 times

3

Doing an exercises I came across the following parameter passage in sort():

let notas = [7,6,10]

notas.sort((a,b) => a < b ? 1 : -1)

console.log(notas)

I could not understand, since I have three values in array and in the parameter is being compared two, and also what means "1" and "-1".

1 answer

4


The method sort() will classify a data collection through a criterion that will define the order that will be established in the object and it needs to determine whether a data is larger or smaller. No matter how you get to which one is bigger, it can be something simple or it can be something complex. If you pass anything to this method there is a standard criterion adopted which is to analyze whether an object is larger than another of the basic forms of the data type.

If you want a different criterion you have to pass a function with a code that defines the criterion. There are several ways to do this, but today the most recommended and simple for most cases is to use the Arrow Function.

This technique is called callback.

The method sort() will call this function at each step of the ordering that it needs to decide whether the element should change place or not. For this it will pass two values, the current element and the next one, and then you compare them as you want. In the example they were received by the parameters a and b.

To inform the situation of one element in relation to the other you must write it in a way that returns 0 if the elements are equal. Return a positive value greater than 0 if the first element (the a) is greater than the second (the b), and a negative number if the first is less than the second. Remembering that the criterion is yours, you can do any crazy if you want. And note that it doesn’t need to be 1 or -1, it just needs to be positive or negative (or neutral).

The specific code of the example is using a conditional operator to make that decision. You are returning 1 when the a is less than b. It seems strange knowing what’s in the previous paragraph, but it’s like that, you want a classification in reverse order, so you have to reverse the value. And the function did not care if it is equal, treats as if the first is larger than the second even if it is equal, and returns -1.

If you want to sort randomly (just to show that you can use any criteria at all:

notas = [7, 6, 10];
notas.sort((a, b) => Math.random() - 0.5);
console.log(notas);

I put in the Github for future reference.

This can return a positive or negative value for each step, so it is random.

In some cases you can do something more complex, for example take one of the members of an object to use as a comparison. Let’s say an object has a property called idade, You can sort the list of objects according to the age of the participants. You will receive the whole object and have to take the property to make the comparison, there it is equal to as if you had received a number directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.