There is a table, even for a "synthetic" example - just a table of integers. Numbers can be repeated and cannot be NULL. It is necessary to display a sorted list in ascending "distance".
Definition of "distance"
Dij = ABS(ABS(Ti)-ABS(Tj))
Where:
Dij- the distance between the i-th and j-th elementTi,Tj- the i-th and j-th elements of the table
Sorting rules
Dij<Djk- If several pairs apply for the next iteration of sorting to the place of the next pair (
Dij==Djk), the pair that has a number is selected - the smallest of all the numbers from these pairs - possibly
Ti<Tj
Amendment
"Increasing the distance" - most likely not quite the correct wording. Because the choice of the next pair depends on the previous one. In other words, at the next iteration, the second element of the previous pair becomes the first in the current one. Therefore, the "distance" can "dance." Type 2-3-10-1-2-2-7-3 ...
For the test
CREATE TABLE Test ( Digit INT NOT NULL ); INSERT INTO Test (Digit) VALUES (17),(16),(9),(8),(7),(3),(0),(-5),(-10),(-17); The desired sorting order:
-17 17 16 -10 9 8 7 -5 3 0