| by Arround The Web | No comments

Scala SortBy

Ordering or organizing the components in a sequential or alphabetical sequence is the process of sorting. A unique sorting function for both mutable and immutable Scala collections is developed and is referred to as Scala Sort. One or more attributes of a Scala collection can be sorted using the SortyBy function. It uses a function that is specified on the user side to sort the components of a collection. The SeqLike trait includes it. To sort the collection according to our needs, we can utilize a variety of data structures and the SortBy function. We will study the Scala SortBy on this topic.

Syntax of the SortBy Function in Scala in Ubuntu 20.04

The SortyBy function sorts the elements of a collection list that is either mutable or immutable. Internally, it makes use of the merge sort algorithm which separates the element list into sections and sorts across them. Unless a sorted collection result is reached, this continues recursively. The SortBy function accepts several characteristics to sort the collection with the components arranged according to the first attribute first, then the second, and so on.

def sortBy[Y](f: (X) ⇒ Y)(implicit order: math.Ordering[Y]): Repr

This SortBy requires two parameters: the function to use and the attribute to sort by.

Example 1: Using SortBy for Sorting an Array in Scala

The SortBy method can be used to order the objects according to one or more class attributes in Scala. If the scope contains an Ordering field type, we can utilize this approach. The ascending order is also the normal sorting order in this case. 

We have an object “Data1” in which we created the case class as the “doctor”. The “doctor” case class has three attributes which are called in the “doctor” constructor. The attributes are given as “id”, “name”, and “salary”. The data types of attributes are also assigned. Then, we established the main method where we defined three variables – “d1”, “d2”, and “d3”. These variables invoked the case class “doctor” and set the values of the case class doctor attribute for each variable.

We declared another variable “NullList” which is kept empty initially. First, we sorted the list based on the “name” attribute which is called inside the SortBy function. Next, we printed the sorted list based on the “salary” attribute.  The SortBy function is set inside the println function. When the data is sorted, it will be printed.

The output shows both sorted lists generated by the SortBy function based on the “name” and “salary” attributes.

Example 2: Using SortBy for Sorting an Array in Reverse Order in Scala

In the previous code, we utilized the SortBy function to sort an array and it sorts the array in ascending order. By slightly altering the procedure, we may put the data in descending order as follows:

Inside the “Data2”, we have a “fruit” class with the defined attributes “id”, “name”, and “price”. Each attribute is set with the data type. Then, we set the main method where we assigned three variables – “f1”, “f2”, and “f3”. The “fruit” class is invoked inside each variable. We assigned the attribute’s value to it.

Next, we generated the empty list which is initialized inside the “Emp_List” variable. We assigned the SortBy function for sorting the data in reverse order to the println statement.

The sorted data is obtained based on the “name” attribute in descending order as shown in the following prompt:

Example 3: Using SortBy for Sorting By the Second Attribute in Scala

This approach, which sorts the collected data on the first attribute, is also helpful for grouping the data based on numerous attributes. In this case, the second attribute is used to sort the first attribute if its values are the same.

Within our object “Data3”, we defined the case class “student”. The constructor is created for the case class “student” which takes the “id”, “name”, and “marks” attributes. Each attribute has the associated data type. Then, we set the values against each attribute in our main method. There are six variables declared as “s1”, “s2”, “s3”, “s4”, “s5”, and, “s6”. Within these variables, we called the “student” class and passed the values for each attribute according to the specified type.

After that, we have a variable “S_List” where the empty is created. Then, we have a println statement where the SortBy function is called. We provided two attributes, “name” and “marks”, to the SortBy function.

As you can see in the output, we repeated the names of the students. So, the SortBy function sorted the data according to the second attribute which is “marks”.

Example 4: Using SortBy for Sorting a Map Value in Scala

Using the SortBy technique, we may order the map by key, either from low to high or from high to low. Let’s attempt to better comprehend it using an example:

To use the ListMap, we imported the Scala package for it. Then, we created the “Data4” object and called the definition of the main method inside it. Next, within the main section, we declared the variable “map” and set the map function in it.

The map function has three key/value pairs. To sort the map by value, we established another variable “result” and called the ListMap. In the ListMap, we have the toSeq.sortBy function that sorts the map by value in a map sequence.

From the SortBy function, we sorted the map by value in a Scala map sequence. The outcome for the sorted map is displayed in the following screenshot of the terminal:

Conclusion

From the aforementioned article, we learned about the SortBy functions in the Scala object-oriented programming paradigm. We discovered that by using the SortBy method, we could create an ordered, sorted collection with just one line of code. We learned how the Scala SortBy method works with several attributes to sort the data appropriately. As a result, the Scala SortBy provides a clear and effective method for sorting in Scala.

Share Button

Source: linuxhint.com

Leave a Reply