sorting – Sort the array by multiple conditions

Question:

Input: array of integers ( -2^31 <= n < 2^31 )

Output: an array of numbers sorted in the following order:

  • non-negative even
  • negative odd
  • non-negative odd
  • negative even

If several numbers fall into one group, then sort them in ascending order

This is code golf, but no answer will be accepted. the purpose of the question is to collect a short solution in each language

please use the site https://tio.run/ so you can easily run your code


Вход:
[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

Выход:
[0, 2, 4, -5, -3, -1, 1, 3, 5, -4, -2]

Таблица лидеров:

execute("ru.stackoverflow.com", "1236112");
.cssload-container,.cssload-cube{width:97px;height:97px;transform-style:preserve-3d}.cssload-container,.cssload-cube,.cssload-half1,.cssload-half2{transform-style:preserve-3d}.cssload-container{position:relative;margin:23px 84px;perspective:292px}.cssload-cube{animation:cube 11.5s forwards infinite;transform-origin:center 49px}.cssload-half1,.cssload-s1{top:0;transform-origin:50% 100%}.cssload-half1{height:39px;position:absolute;animation:half-fold 11.5s forwards infinite}.cssload-side{width:19px;height:19px;background:#ddd;position:absolute}.cssload-s1{left:39px;animation:s1ani 11.5s forwards infinite}.cssload-s2,.cssload-s3,.cssload-s4{left:39px;transform-origin:50% 0}.cssload-s2{top:19px;animation:s2ani 11.5s forwards infinite}.cssload-s3{top:39px;animation:s3ani 11.5s forwards infinite}.cssload-s4{top:58px;animation:s4ani 11.5s forwards infinite}.cssload-s5{left:19px;top:19px;transform-origin:100% 50%;animation:s5ani 11.5s forwards infinite}.cssload-s6{left:58px;top:39px;transform-origin:0 50%;animation:s6ani 11.5s forwards infinite}@keyframes cube{0%,30%{transform:rotateX(0)}40%{transform:rotateX(45deg) rotateY(0) rotate(45deg)}60%{transform:rotateX(60deg) rotateY(0) rotate(45deg)}65%,70%{transform:rotateX(60deg) rotate(45deg) rotate(180deg)}75%,80%{transform:rotateX(60deg) rotate(45deg) rotate(1turn)}90%{transform:rotateX(0) rotate(0) rotate(0)}}@keyframes s1ani{0%{opacity:1;transform:translateY(0);background:#ddd}40%{transform:rotateX(0);background:#ddd}50%{transform:rotateX(-90deg);background:#ddd}90%{transform:rotateX(-90deg)}}@keyframes s2ani{0%{opacity:0;transform:rotateX(-179deg)}10%{opacity:1;transform:rotateX(0)}40%{background:#ddd}45%,80%{background:#b4b4b4}65%{opacity:1;background:#b4b4b4}90%{opacity:1}to{opacity:0}}@keyframes s3ani{0%,10%{opacity:0;transform:rotateX(-179deg)}20%,90%{opacity:1;transform:rotateX(0)}40%{background:#ddd}45%{background:#969696}to{opacity:0}}@keyframes s4ani{0%,20%{opacity:0;transform:rotateX(-179deg)}10%,to{opacity:0}30%{opacity:1;transform:rotateX(0)}40%{transform:rotateX(0);background:#ddd}50%{transform:rotateX(90deg);background:#b4b4b4}80%{background:#b4b4b4}90%{opacity:1;transform:rotateX(90deg)}}@keyframes s5ani{0%,10%{opacity:0;transform:rotateY(-179deg)}20%{opacity:1;background:#ddd;transform:rotateY(0)}40%{transform:rotateY(0)}50%{transform:rotateY(90deg)}55%{background:#ddd}60%{background:#c8c8c8}90%{transform:rotateY(90deg);opacity:1}to{opacity:0}}@keyframes s6ani{0%,20%{opacity:0;transform:rotateY(179deg)}30%{opacity:1;transform:rotateY(0)}40%{transform:rotateY(0)}50%{transform:rotateY(-90deg);background:#ddd}60%,80%{background:#c8c8c8}90%{opacity:1;transform:rotateY(-90deg)}to{opacity:0}}@keyframes half-fold{0%,50%{transform:rotateX(0)}60%,90%{transform:rotateX(-90deg)}}
<script src="https://mayorovp.github.io/codegolf/table-8c505e68f1349e4c69e7.js"></script>
<div class=cssload-container><div class=cssload-cube><div class=cssload-half1><div class="cssload-side cssload-s1"></div><div class="cssload-side cssload-s2"></div><div class="cssload-side cssload-s5"></div></div><div class=cssload-half2><div class="cssload-side cssload-s3"></div><div class="cssload-side cssload-s4"></div><div class="cssload-side cssload-s6"></div></div></div></div>

Answer:

perl, 56

I hate to support the myth that perl is the most incomprehensible language, but …

$,=", ";
@X = (-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5);
print sort{($a<0^$a&1).($a<0)cmp($b<0^$b&1).($b<0)||$a<=>$b}@X;

The sort function sorts the end-of-line array ( @X ) using the block of code before it. In this block, the predefined variables $a and $b containing the compared values ​​are available. the task of the block is to calculate which of them is greater.

sort {
    Выражение1 cmp Выражение2   # Лексикографическое сравнение двух выражений
    ||                          # ИЛИ (если они равны)
    $a <=> $b                   # Числовое сравнение самих переменных
} @X;

Now the expression itself for evaluating the weight of a variable:

( $a<0 ^ $a & 1 )   # XOR между значениями признака отрицательного числа и его четности (младшего бита)
                    # он равен 0 для неотрицательных четных и отрицательных нечетных, иначе 1
.                   # Конкатенация строк
( $a<0 )            # признак отрицательности
                    # в perl сравнение дает пустую строку если условие ложно или 1 если истинно

At the output, this expression gives the strings: "0", "01", "1", "11". In lexicographic order, these lines give us the order of conditions we need.

Example at tio.run

Scroll to Top