Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Syntax
@function sort($list) {
// Implementation using loops and conditional statements
@return $sorted-list;
}
Implementation Using Bubble Sort
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.
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
<!DOCTYPE html>
<html>
<head>
<style>
@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;
}
$list: 10, 5, 3, 7, 2, 8;
$sorted-list: sort($list);
@each $num in $sorted-list {
.number-#{$num} {
width: #{$num * 10}px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
}
</style>
</head>
<body>
<div class="number-2">2</div>
<div class="number-3">3</div>
<div class="number-5">5</div>
<div class="number-7">7</div>
<div class="number-8">8</div>
<div class="number-10">10</div>
</body>
</html>
.number-2 {
width: 20px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
.number-3 {
width: 30px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
.number-5 {
width: 50px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
.number-7 {
width: 70px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
.number-8 {
width: 80px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
.number-10 {
width: 100px;
height: 30px;
background-color: #4CAF50;
margin: 5px;
display: inline-block;
color: white;
text-align: center;
line-height: 30px;
}
Green boxes with varying widths (20px to 100px) appear on the page, each displaying the sorted numbers from 2 to 10.
How It Works
The function takes a list of numbers and sorts them in ascending order using the bubble sort algorithm. 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. The process repeats until the list is completely sorted.
Conclusion
Sass doesn't provide any built-in sorting function, but it is possible to write custom sorting functions using control directives like @for and @while loops combined with list manipulation functions. These custom functions can be useful for generating dynamic CSS based on sorted data.
