1

Please help on this how to achieve on JavaScript. I have data type . The aim of this sorting is I have object columns where columns are not arranged as it required. This function below simply arrange array column for example. I have tested this code in my local machine and this is the output that I have got.

<?php
$data =  '[
  {
    "from": "Barcelona",
    "to": "New York",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "B33",
    "seatno": "Y15"
  },
  {
    "from": "Barcelona",
    "to": "Gerona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Madrid",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "New York",
    "to": "Stockholm",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Gerona",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  }
]';

Now I need the data sorted in this way flight from Madrid to Barcelona then Barcelona to Gerona - Gerona to Barcelona - Barcelona to New York - New York to Stockholm -

$data_decode = json_decode($data, true);

usort($data_decode, function ($a, $b) {
  return $a['to'] === $b['from'] ? 0 : 1;
});


print_r($data_decode);

Output

Array ( [0] => Array ( [from] => Madrid [to] => Barcelona [instruction] => [time] => 2018-02-02 20:05 [transport] => Bus [transportno] => M31, M32, M33 [seatno] => Any )

[1] => Array
    (
        [from] => Barcelona
        [to] => Gerona
        [instruction] => 
        [time] => 2018-02-02 20:05
        [transport] => Bus
        [transportno] => M31, M32, M33
        [seatno] => Any
    )

[2] => Array
    (
        [from] => Gerona
        [to] => Barcelona
        [instruction] => 
        [time] => 2018-02-02 20:05
        [transport] => Bus
        [transportno] => M31, M32, M33
        [seatno] => Any
    )

[3] => Array
    (
        [from] => Barcelona
        [to] => New York
        [instruction] => 
        [time] => 2018-02-02 20:05
        [transport] => Flight
        [transportno] => B33
        [seatno] => Y15
    )

[4] => Array
    (
        [from] => New York
        [to] => Stockholm
        [instruction] => 
        [time] => 2018-02-02 20:05
        [transport] => Flight
        [transportno] => M31, M32, M33
        [seatno] => Any
    )

) [Finished in 0.1s]

How I can get this result in JavaScript. You help will be appreciated.

  • I think you are trying to do sorting an array of objects by key, look at: https://stackoverflow.com/questions/8837454/sort-array-of-objects-by-single-key-with-date-value – dee cue Aug 05 '20 at 16:23
  • I tried exactly the way that you did not it's not giving the expected result. Please check the above output from php usort code. Because it's should give chaining like Barcelona to Gerona - Gerona to Barcelona - Barcelona to New York - New York to Stockholm - data is exactly presented for this reason but each Airport is connected to each other you can check the php output –  Aug 05 '20 at 17:32

1 Answers1

0

In javascript see the array sort method

var data = [
  {
    "from": "Barcelona",
    "to": "New York",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "B33",
    "seatno": "Y15"
  },
  {
    "from": "Barcelona",
    "to": "Gerona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Madrid",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "New York",
    "to": "Stockholm",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Gerona",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  }
];

var sortedData = data.sort( function( a, b ) {
  return a[ 'to' ] === b[ 'from' ] ? 0 : 1;
} );

console.log( sortedData );

Output


[
  {
    "from": "Madrid",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Barcelona",
    "to": "Gerona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Gerona",
    "to": "Barcelona",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Bus",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  },
  {
    "from": "Barcelona",
    "to": "New York",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "B33",
    "seatno": "Y15"
  },
  {
    "from": "New York",
    "to": "Stockholm",
    "instruction": "",
    "time": "2018-02-02 20:05",
    "transport": "Flight",
    "transportno": "M31, M32, M33",
    "seatno": "Any"
  }
]
Levi Cole
  • 3,561
  • 1
  • 21
  • 36
  • I tried exactly the way that you did not it's not giving the expected result. Please check the above output from php usort code. Because it's should give chaining like Barcelona to Gerona - Gerona to Barcelona - Barcelona to New York - New York to Stockholm - data is exactly presented for this reason but each Airport is connected to each other you can check the php output. –  Aug 05 '20 at 17:30
  • @user3201528 Please can you expand on the issue. I have pasted the output of the above code and appears to be correct. – Levi Cole Aug 05 '20 at 17:41
  • Hi. I am not receiving same output as you have posted, When I run the code snippet then I get the different output please check this link , What I am receiving https://github.com/qdcmedia123/test123/blob/master/sorttest.js –  Aug 06 '20 at 06:49
  • @user3201528 you need to log the `sortedData` variable not the `data` var. Change to `console.log( sortedData )`. Hope this helps. – Levi Cole Aug 06 '20 at 06:59
  • I have tried but still same please check the link github.com/qdcmedia123/test123/blob/master/sorttest.js –  Aug 06 '20 at 13:08
  • Hi Any idea, Why I am not getting same output as you, When executing the JS Script. –  Aug 09 '20 at 06:52
  • @user3201528 your code appears to be working fine for me. How/where are you executing your code? – Levi Cole Aug 10 '20 at 09:21
  • I am using this code in Sublime Text to execute js, nodejs terminal as well and I have tried to execute that in https://developer.mozilla.org/ as they have a lot of example their but everywhere I am getting unexpected output. Long time ago it used to work for me. –  Aug 11 '20 at 13:02