Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----
- // libsass (v3.2.5)
- // ----
- // Declare the default sort order. Use Sass's !default flag so this
- // value doesn't override the variable if it was already declared.
- $default-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;
- @function str-compare($string-a, $string-b, $order: $default-sort-order) {
- // Cast values to strings
- $string-a: to-lower-case($string-a + unquote(""));
- $string-b: to-lower-case($string-b + unquote(""));
- // Loop through and compare the characters of each string...
- @for $i from 1 through min(str-length($string-a), str-length($string-b)) {
- // Extract a character from each string
- $char-a: str-slice($string-a, $i, $i);
- $char-b: str-slice($string-b, $i, $i);
- // If characters exist in $order list and are different
- // return true if first comes before second
- @if $char-a and $char-b and index($order, $char-a) != index($order, $char-b) {
- @return index($order, $char-a) < index($order, $char-b);
- }
- }
- // In case they are equal after all characters in one string are compared,
- // return the shortest first
- @return str-length($string-a) < str-length($string-b);
- }
- @function swap($list, $index-a, $index-b) {
- @if abs($index-a) > length($list) or abs($index-b) > length($list) {
- @return $list;
- }
- $tmp: nth($list, $index-a);
- $list: set-nth($list, $index-a, nth($list, $index-b));
- $list: set-nth($list, $index-b, $tmp);
- @return $list;
- }
- @function sort($list, $order: $default-sort-order) {
- // Cycle through each item in the list
- @for $i from 1 through length($list) {
- // Compare the item with the previous items in the list
- @for $j from $i * -1 through -1 {
- // abs() trick to loop backward
- $j: abs($j);
- // Compare both values
- @if $j > 1 and str-compare(nth($list, $j), nth($list, $j - 1), $order) {
- // If the item should go before the other, swap them
- $list: swap($list, $j, $j - 1);
- }
- }
- }
- // Return the sorted list
- @return $list;
- }
- $list: oranges pears apples strawberries bananas;
- $sort: sort($list);
- // => apples bananas oranges pears strawberries
- @each $listItem in $list {
- .list_#{$listItem} {
- content: "#{$listItem}";
- }
- }
- @each $listItem in $sort {
- .sort_#{$listItem} {
- content: "#{$listItem}";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement