Sorting Function in SASS


In this article, we will learn about the sorting function in Sass, but before moving forward, let’s have a basic idea about Sass; so sass is a powerful and popular preprocessor language for CSS that allows developers to write more efficient and maintainable stylesheets. One of the best advantages of Sass is the ability to use functions to streamline the development process. However, one function that Sass does not provide out of the box is a sorting function.

Sorting is a common task in all programming languages and can be useful in many different contexts when working with stylesheets. Unfortunately, Sass doesn’t provide any built-in function for sorting, but there are several workarounds that developers can use to achieve the desired result.

One approach to sorting in Sass is to use loops and conditional statements. This method involves creating a loop that iterates over the list to be sorted, comparing each item to the next one in the list and swapping them if necessary. This process is repeated until the entire list is sorted; in this article, we will use a bubble sort algorithm for sorting using loops and functions.

Here's an example of how to implement a simple sorting function in Sass using the Bubble sort technique −

Example

This SCSS code defines a function sort($list) that sorts a list of numbers in ascending order and returns the sorted list. The function uses a simple implementation of the bubble sort algorithm.

So let’s know how it works, first, the function takes a list of numbers and sorts them in ascending order; it uses a while loop and a for loop with an if statement to compare each pair of adjacent numbers in the list. If they are out of order, it swaps them using temporary variables. It then repeats the process until the list is sorted.

@function sort($list) {
   $len: length($list);
   $sorted: false;
   @while not $sorted {
      $sorted: true;
      @for $i from 1 to ($len - 1) {
         $j: $i + 1;
         @if nth($list, $i) > nth($list, $j) {
            $temp: nth($list, $i);
            $list: set-nth($list, $i, nth($list, $j));
            $list: set-nth($list, $j, $temp);
            $sorted: false;
         }
      }
      $len: $len - 1;
   }
   @return $list;
}

The below part of the code uses an @each loop to generate CSS code for each number in the sorted list, creating a CSS class with a width property set to the value of the number multiplied by 10 pixels.

$list: 10, 5, 3, 7, 2, 8;
$sorted-list: sort($list);

@each $num in $sorted-list {
   .number-#{$num} {
      width: #{$num * 10}px;
   }
}

Output

.number-2 {
   width: 20px;
}
.number-3 {
   width: 30px;
}
.number-5 {
   width: 50px;
}
.number-7 {
   width: 70px;
}
.number-10 {
   width: 100px;
}
.number-8 {
   width: 80px;
}

Conclusion

In this article, we learned about the Sorting Function in SASS and knew that Sass doesn’t provide any built-in sorting. However, it is possible to write custom sorting functions in SASS using a combination of control directives such as @for and @while loops and list manipulation functions such as length(), nth(), and set-nth(). These custom functions can be useful for sorting lists of any data type, including numbers, strings, or objects, and can generate dynamic CSS code based on the sorted list.

Updated on: 11-May-2023

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements