'how to sort an array with a specific logic

I wish to sort the following array in such way that the 'distance' between the events are the largest.

example:

events =  [[1,2],[2,8],[3,4],[1,2]]

the end result should be:

events =  [[1,2],[1,2],[3,4],[2,8]]

why? consider [1:2] where x=1 and y=2 so x must comes first unless if y-x is larger then it should be pushed further away. in the example it has [2:8] which means the distance is 6

what have I done?

events = sorted(events, key = lambda x: x[0])

result:

[[1, 2], [1, 2], [2, 8], [3, 4]]

but I couldnt figure our how to add another logic for the distance



Solution 1:[1]

You can sort by two different things by making the key into a tuple. Is this what you're looking for?

events = sorted(events, key = lambda x: (x[1] - x[0], x[0]))

This sorts primarily such that those pairs with a larger difference will be at the end of the list and those with a smaller difference at the start. If negative differences can occur those will be the first. Secondarily, pairs with the same distance are sorted according to the first value.

Solution 2:[2]

Its because 'Access-Control-Allow-Origin' header is not present in the response.

Try to add Access-Control-Allow-Origin: *

Header in the node api response and It should work fine

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Nitin Dev