Resolved: How to sort an array of objects without sequentially repeating two specific properties in Javascript?

0

Good evening guys, I'm breaking my head here, but I could not come up with a solution ... next, I have an array of objects:

[
    {order: 0, from: 'Birigui', to: 'Penápolis'},
    {order: 1, from: 'Birigui', to: 'Araçatuba'},
    {order: 2, from: 'Avanhandava', to: 'Penápolis'},
    {order: 3, from: 'Avanhandava', to: 'Araçatuba'},
    {order: 4, from: 'Penápolis', to: 'Araçatuba'},
    {order: 5, from: 'Birigui', to: 'Avanhandava'},
    {order: 6, from: 'Marilia', to: 'São Paulo'},
    {order: 7, from: 'Marilia', to: 'Birigui'},
    {order: 8, from: 'Marilia', to: 'Penápolis'},
]

I need to sort this array without repeating the 'from' and 'to' fields sequentially.

For example, if the first item in the list contains 'Birigui' and 'Penápolis', the second item can not contain 'Birigui' and 'Penápolis' in either property.

The ordered list would look like this:

[
    {order: 0, from: 'Birigui', to: 'Penápolis'},
    {order: 1, from: 'Marilia', to: 'São Paulo'},
    {order: 2, from: 'Birigui', to: 'Araçatuba'},
    {order: 3, from: 'Avanhandava', to: 'Penápolis'},
    {order: 4, from: 'Marilia', to: 'Birigui'},
    {order: 5, from: 'Penápolis', to: 'Araçatuba'},
    {order: 6, from: 'Birigui', to: 'Avanhandava'},
    {order: 7, from: 'Marilia', to: 'Penápolis'},
    {order: 8, from: 'Avanhandava', to: 'Araçatuba'},
]

I put a few places to be an example, but the minimum number of places on the list would be 10.

Does anyone have an idea how to do this?

Resolved: Solution

    
asked by anonymous 07.12.2018 / 23:10

0 answers