Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. // ----
  2. // libsass (v3.2.5)
  3. // ----
  4.  
  5. // Declare the default sort order. Use Sass's !default flag so this
  6. // value doesn't override the variable if it was already declared.
  7. $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;
  8.  
  9. @function str-compare($string-a, $string-b, $order: $default-sort-order) {
  10. // Cast values to strings
  11. $string-a: to-lower-case($string-a + unquote(""));
  12. $string-b: to-lower-case($string-b + unquote(""));
  13.  
  14. // Loop through and compare the characters of each string...
  15. @for $i from 1 through min(str-length($string-a), str-length($string-b)) {
  16.  
  17. // Extract a character from each string
  18. $char-a: str-slice($string-a, $i, $i);
  19. $char-b: str-slice($string-b, $i, $i);
  20.  
  21. // If characters exist in $order list and are different
  22. // return true if first comes before second
  23. @if $char-a and $char-b and index($order, $char-a) != index($order, $char-b) {
  24. @return index($order, $char-a) < index($order, $char-b);
  25. }
  26. }
  27.  
  28. // In case they are equal after all characters in one string are compared,
  29. // return the shortest first
  30. @return str-length($string-a) < str-length($string-b);
  31. }
  32.  
  33. @function swap($list, $index-a, $index-b) {
  34. @if abs($index-a) > length($list) or abs($index-b) > length($list) {
  35. @return $list;
  36. }
  37. $tmp: nth($list, $index-a);
  38. $list: set-nth($list, $index-a, nth($list, $index-b));
  39. $list: set-nth($list, $index-b, $tmp);
  40. @return $list;
  41. }
  42.  
  43. @function sort($list, $order: $default-sort-order) {
  44.  
  45. // Cycle through each item in the list
  46. @for $i from 1 through length($list) {
  47.  
  48. // Compare the item with the previous items in the list
  49. @for $j from $i * -1 through -1 {
  50.  
  51. // abs() trick to loop backward
  52. $j: abs($j);
  53.  
  54. // Compare both values
  55. @if $j > 1 and str-compare(nth($list, $j), nth($list, $j - 1), $order) {
  56. // If the item should go before the other, swap them
  57. $list: swap($list, $j, $j - 1);
  58. }
  59. }
  60. }
  61.  
  62. // Return the sorted list
  63. @return $list;
  64. }
  65.  
  66. $list: oranges pears apples strawberries bananas;
  67. $sort: sort($list);
  68. // => apples bananas oranges pears strawberries
  69.  
  70. @each $listItem in $list {
  71. .list_#{$listItem} {
  72. content: "#{$listItem}";
  73. }
  74. }
  75.  
  76. @each $listItem in $sort {
  77. .sort_#{$listItem} {
  78. content: "#{$listItem}";
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement