Given an array of countries with offices opened, how do I sort the array in order from the earliest date recorded to the latest date recorded?
The original array is
countriesList = [
{ country: 'USA',
officeOpened: [ {date: '2016-04-01', city: 'Denver'}],
officeClosed: [ {date: '2016-10-01', city: 'NY'}]
},
{ country: 'UK',
officeOpened: [{date: '2023-05-01', city: 'London'}],
officeClosed: []
},
{ country: 'China',
officeOpened: [{date: '2015-01-01', city: 'Shanghai'}],
officeClosed: []
},
{ country: 'No-office',
officeOpened: [],
officeClosed: []
},
{ country: 'Many-offices',
officeOpened: [{date: '2023-04-01', city: 'A'} , {date: '2023-06-01', city: 'B'}],
officeClosed: []
}
]
Expected output:
countriesList = [
{ country: 'China',
officeOpened: [{date: '2015-01-01', city: 'Shanghai'}]
},
{ country: 'USA',
officeOpened: [ {date: '2016-04-01', city: 'Denver'}],
officeClosed: [ {date: '2016-10-01', city: 'NY'}]
},
{ country: 'Many-offices',
officeOpened: [{date: '2023-04-01', city: 'A'} , {date: '2023-06-01', city: 'B'}],
officeOpened: []
},
{ country: 'UK',
officeOpened: [{date: '2023-05-01', city: 'London'}]
},
{ country: 'No-office',
officeOpened: [],
officeOpened: []
}
]
So I want to sort the country object in this array. Notice how the earliest date record was in China, so the whole China object is sorted to the first place, then comes the second earliest date recorded in the USA, the UK. How do I do this?
I’ve seen some similar question, but not exactly with this type of data structure.
Edit: if it’s relevant, officeOpened/Closed can also be empty, in which case, it can either be first or last in the array. And as one of the answer points out, we only need to care about and sort base on the first office opened in a country (the first object in officeOpened/Closed array because these arrays are sorted beforehand.)
3
Answers
If we can assume that 1. officeOpened will always be before officeClosed, and 2. each officeOpened will always have an element in the array, and 3. There will only be one element in the officeOpened array or we only care about the first element, then we can do:
You could take the date and sort by string.
As
officeOpened
is an array, I assume this array can have multiple entries for each country. If yes, the following searches the earliest date for each countries and then sorts by this date.The
earliestDate
function searches for the earliest date in all arrays defined in theprops
array. If none exists, "9999" is returned, so that country is moved to the end of the list.Of course this implementation is not the fastest one, as it recalculates the
earliestDate
in every comparison of the sort. Also usingsort
to find the minimum of an array is inefficient. But you should get the basic idea on how to sort your values.A better implementation would call
earliestDate
once for every element in the array and reuse this value. Also finding the minium date viasort
is not optimal, a faster way would just be a simple iteration. But I leave that as exercise …