Sorting by two columns conditionally Column a has category ids, column b has set orders:

+-------------+-----------+
| category_id | set_order |
+-------------+-----------+
|          80 | null      |
|          81 | null      |
|          82 | null      |
|          83 | 2         |
|          84 | null      |
|          85 | 4         |
|          86 | null      |
|          87 | null      |
|          88 | null      |
|          89 | null.     |
|          90 | null      |
+-------------+-----------+


Desired output is:

+----+
| 90 |
| 83 |
| 89 |
| 85 |
| 88 |
| 87 |
| 86 |
| 84 |
| 82 |
| 81 |
| 80 |
+----+


Please help how to write the order by clause. 83 has value 2 in column b, hence is to appear 2nd in ordering. 85 has value 4 in column b, hence is to appear 4th in ordering.
This is a job better done on application layer. Databases are not suited for this. Just do 

SELECT category_id, set_order FROM your_table ORDER BY category_id DESC;


Then write a function that rearranges the array returned by above query. 

For fun I did it in MySQL, just to let you see, how complicated this can be. So you see it's doable, note though, that this can be horrible performance-wise.

Sample data:

CREATE TABLE t
    (`category_id` int, `set_order` int)
;

INSERT INTO t
    (`category_id`, `set_order`)
VALUES
    (80, NULL),
    (81, NULL),
    (82, NULL),
    (83, '2'),
    (84, NULL),
    (85, '4'),
    (86, NULL),
    (87, NULL),
    (88, NULL),
    (89, NULL),
    (90, NULL)
;


Query:

SELECT category_id FROM (
    SELECT
    category_id, set_order
    , IF(set_order IS NULL, @newOrder := IF(@newOrder + 1 IN (SELECT set_order FROM t WHERE set_order IS NOT NULL), @newOrder + 2, @newOrder + 1), NULL) new_order
    FROM t,
    (SELECT @newOrder := 0) var_init_subquery_alias
    ORDER BY category_id DESC
) sq ORDER BY COALESCE(new_order, set_order);


Result:

+-------------+
| category_id |
+-------------+
|          90 |
|          83 |
|          89 |
|          85 |
|          88 |
|          87 |
|          86 |
|          84 |
|          82 |
|          81 |
|          80 |
+-------------+