Sorting Function In SASS

The main thing in any sorting function is the ability to compare two strings and check which of the two must go before the other. Almost every programming language has some in-built sort functions, but to do this in Sass you need to create your own string comparison function. In the first step, we need to teach Sass, the correct order of sorting the strings based on the characters each string contains. We do this using a variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

$sort-order: a b c d e f g h i j k l m n o p 
             q r s t u v w x y z !default;

chevron_right


This will be used to declare that strings that begin with a should appear before strings that begin with b or c and so on. You may also add other values like numbers, capital letters or symbols.
Now, you will be needing a comparison function. In the comparison function we basically loop across the characters in each string and look up their order in the sort-order list with the help of the Sass index() function. It will give us two values that can be compared which one of them goes before the other. If the values are same, we loop around the next set of characters, and so on. 
The str-compare() function returns true if $string-a goes before $string-b and false if it does not. 
 

filter_none

edit
close

play_arrow

link
brightness_4
code

@function compare($string-a, $string-b, $sort-order) {
  $string-a: to-lower-case($string-a + unquote(""));
  $string-b: to-lower-case($string-b + unquote(""));      
  
  @for $i from 1 through min(
          str-length($string-a), str-length($string-b)) {
  
    $char-a: str-slice($string-a, $i, $i);
    $char-b: str-slice($string-b, $i, $i);
      
    @if $char-a and $char-b and index(
           $order, $char-a) != index($sort-order, $char-b) {
      @return index(
           $sort-order, $char-a) < index($sort-order, $char-b);
    }
  }
    
  @return str-length($string-a) < str-length($string-b);
}

chevron_right


Here, we will be using Bubble Sort algorithm for sorting as it is the most basic and easy approach. Since bubble sort is based on swapping two values in the array or list, we will also be needing a swap() function in Sass. 
The swap() function will take a list and two index values as input. The index values need to be swapped in the list.
We will be using the set-nth() function, as it simply just updates the list rather than creating a new list. 
 

filter_none

edit
close

play_arrow

link
brightness_4
code

@function swap($list, $index-a, $index-b) {
  @if abs($index-a) > length($list) or
               abs($index-b) > length($list) {
    @return $list;
  }
  $temp: nth($list, $index-a);
  $list: set-nth($list, $index-a, nth($list, $index-b));
  $list: set-nth($list, $index-b, $temp);
  @return $list;
}

chevron_right


Now, we have a compare() function as well as a swap() function, meaning we are ready to create our sort() function. The function simply loops through the list, compares items with each other and swaps them once compared until the list is completely sorted. 
 

filter_none

edit
close

play_arrow

link
brightness_4
code

@function sort($list, $sort-order) {
  @for $i from 1 through length($list) {
  @for $j from $i * -1 through -1 {
      $j: abs($j);
      @if $j > 1 and compare(nth($list, $j),
                  nth($list, $j - 1), $sort-order) {
        $list: swap($list, $j, $j - 1);
      }
    }
  }
  @return $list;
}

chevron_right


Finally we need to pass the string values and return sort() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

$list: internships placements classes geeksforgeeks courses;
$sort: sort($list);

chevron_right


Output:

classes courses geeksforgeeks internships placements



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.