这个问题是这个问题的延续。
假设我有一个如下所示的数组:
const questions = [
{_id: 1, q: 'why?', group: 'no-group', date: '8', selected: false },
{_id: 2, q: 'what?', group: 'group 1', date: '6', selected: false },
{_id: 3, q: 'when?', group: 'no-group', date: '7', selected: false },
{_id: 4, q: 'where?', group: 'group 1', date: '5', selected: false },
{_id: 5, q: 'which?', group: 'group 2', date: '3', selected: false },
{_id: 6, q: 'who?', group: 'no-group', date: '0', selected: false },
{_id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false },
{_id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false },
{_id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false },
{_id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false },
{_id: 11, q: 'when is lunch?', group: 'group-2', date: '10', selected: false },
];
如何编写代码对其进行排序,以便按日期和组对对象进行排序。'no-group'但是,如果:假设组 1 作为第二个对象出现,则以下对象应为按日期排序的组 1,直到列出所有组 1 对象,并且对于除具有组属性值的对象之外的所有其他对象都相同。
因此,对于上面的数组,我应该得到如下输出:
[
{_id: 6, q: 'who?', group: 'no-group', date: '0', selected: false },
{_id: 9, q: 'where is waldo?', group: 'group 1', date: '1', selected: false },
{_id: 8, q: 'who, me?', group: 'group 1', date: '4', selected: false },
{_id: 4, q: 'where?', group: 'group 1', date: '5', selected: false },
{_id: 2, q: 'what?', group: 'group 1', date: '6', selected: false },
{_id: 10, q: 'which way is up?', group: 'no-group', date: '2', selected: false },
{_id: 5, q: 'which?', group: 'group 2', date: '3', selected: false },
{_id: 7, q: 'why not?', group: 'group 2', date: '9', selected: false },
{_id: 11, q: 'when is lunch?', group: 'group 2', date: '10', selected: false },
{_id: 3, q: 'when?', group: 'no-group', date: '7', selected: false },
{_id: 1, q: 'why?', group: 'no-group', date: '8', selected: false },
];
// the spacing is just for easier readability.
互换的青春
HUH函数
肥皂起泡泡
相关分类