$default-order:
  '!' '#' '$' '%' '&' "'" '(' ')' '*' '+' ',' '-' '.' '/' '[' '\\' ']' '^' '_' '{' '|' '}' '~'
  '0' '1' '2' '3' '4' '5' '6' '7' '8' '9'
  '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;

// Compares two string to determine which comes first
//
// @param $a: first string
// @parem $b: second string
// @param $order: order to deal with
//
// @return $boolean

@function str-compare($a, $b, $order) {
  @if type-of($a) == number and type-of($b) == number {
    @return $a < $b;
  }
  $a: to-lower-case($a + unquote(''));
  $b: to-lower-case($b + unquote(''));
  @for $i from 1 through min(str-length($a), str-length($b)) {
    $char-a: str-slice($a, $i, $i);
    $char-b: str-slice($b, $i, $i);
    @if $char-a and $char-b and index($order, $char-a) != index($order, $char-b) {
      @return index($order, $char-a) < index($order, $char-b);
    }
  }
  @return str-length($a) < str-length($b);
}


@function quick-sort($list, $order: $default-order) {
  $less:  ();
  $equal: ();
  $large: ();
  @if length($list) > 1 {
    $seed: nth($list, ceil(length($list) / 2));
    @each $item in $list {
      @if $item == $seed {
        $equal: append($equal, $item, comma);
      }
      @else if str-compare($item, $seed, $order) {
        $less: append($less, $item, comma);
      }
      @else if not str-compare($item, $seed, $order) {
        $large: append($large, $item, comma);
      }
    }
    @return join(join(quick-sort($less, $order), $equal), quick-sort($large, $order));
  }
  @return $list;
}

@function scale-range($ideal: 16, $ratio: 1.333, $important: 0, $offset-important: -4, $offset-ideal: -1) {

  $greek: (alpha beta gamma delta epsilon zeta eta theta iota);
  $numbers: ();

  $greek-length: length($greek);

  $ideal-list: ();
  $important-list: ();
  $tmp-list: ();

  $return-list: ();

  @if $important > 0 {
    $ide: $offset-ideal;
    $imp: $offset-important;

    @while $ide < $greek-length {
      $important-list: join($important-list, modular-scale($important, $ide, $ratio));
      $ide: $ide + 1;
    }

    @while $imp < $greek-length {
      $ideal-list: join($ideal-list, modular-scale($ideal, $imp, $ratio));
      $imp: $imp + 1;
    }

    $tmp-list: map-merge($important-list, $ideal-list);
    $tmp-list: quick-sort($tmp-list);

    @for $i from 1 through $greek-length {
      $numbers: append($numbers, nth($tmp-list, $i));
      $return-list: map-merge($return-list, nth($greek, -$i) nth($numbers, $i), comma);
    }
  }

  @return $return-list;
}
