使用其他数组对对象数组进行排序

时间:2019-08-02 09:06:28

标签: javascript sorting

我创建了这个对象数组:

df2 = df1.groupby('g')['time'].agg(['first','last'])
df2['diff'] = df2['last'].sub(df2['first'])
print (df2)
                first                last     diff
g                                                 
1 2019-06-01 00:00:02 2019-06-01 00:01:06 00:01:04
3 2019-06-01 00:01:38 2019-06-01 00:01:54 00:00:16

我想要一个具有以下顺序的对象的新数组:

const palette = [
  {
    color: 'Blue',
    brightness: 'Soft',
  },
  {
    color: 'Blue',
    brightness: 'Medium',
  },
  {
    color: 'Blue',
    brightness: 'Principal',
  },
  {
    color: 'Magenta',
    brightness: 'Soft',
  },
  {
    color: 'Magenta',
    brightness: 'Medium',
  },
  {
    color: 'Magenta',
    brightness: 'Principal',
  }
]

这就是我想要的结果:

const colorOrder = ['Blue', 'Magenta']
const brightnessOrder = ['Principal', 'Soft', 'Medium']

我尝试使用此功能:

const colors = [
  {
    color: 'Blue',
    brightness: 'Principal',
  },
  {
    color: 'Blue',
    brightness: 'Soft',
  },
  {
    color: 'Blue',
    brightness: 'Medium',
  },
  {
    color: 'Magenta',
    brightness: 'Principal',
  }
  {
    color: 'Magenta',
    brightness: 'Soft',
  },
  {
    color: 'Magenta',
    brightness: 'Medium',
  },
]

我这样称呼它:

function sortArrayByAnotherArray(array: any[], order: number[] | string[], key: string) {
  const newArray = array.slice(0).sort((a, b) => {
    const A = a[key]
    const B = b[key]
    return order.indexOf(A) < order.indexOf(B) ? 1 : -1
  })
  return newArray
}

结果是:

const palette1 = sortArrayByAnotherArray(
  palette,
  brightnessOrder,
  'brightness'
)
const palette2 = sortArrayByAnotherArray(
  palette1,
  colorOrder,
  'color'
)

console.log('\n', palette)

console.log('\n', brightnessOrder)
console.log(palette1)

console.log('\n', colorOrder)
console.log(palette2)

那是一团糟,顺序与数组中的顺序不一样:颜色被反转,亮度值也被反转。 然后,我认为两次(或多次)调用此函数会产生问题。 有办法解决吗?存在一种明智的方式来做我需要的事吗?

3 个答案:

答案 0 :(得分:2)

您可以将所需的订单与logical OR ||和索引的增量相链接。

const
    palette = [{ color: 'Blue', brightness: 'Soft' }, { color: 'Blue', brightness: 'Medium' }, { color: 'Blue', brightness: 'Principal' }, { color: 'Magenta', brightness: 'Soft' }, { color: 'Magenta', brightness: 'Medium' }, { color: 'Magenta', brightness: 'Principal' }],
    colorOrder = ['Blue', 'Magenta'],
    brightnessOrder = ['Principal', 'Soft', 'Medium'];

palette.sort((a, b) => 
    colorOrder.indexOf(a.color) - colorOrder.indexOf(b.color) ||
    brightnessOrder.indexOf(a.brightness) - brightnessOrder.indexOf(b.brightness)
);

console.log(palette);
.as-console-wrapper { max-height: 100% !important; top: 0; }

一种使用功能和给定顺序数组对副本进行排序的方法。

function sortArrayByAnotherArrays(data, orders) {
    const
        getObject = array => array.reduce((r, k, i) => (r[k] = i + 1, r), {}),
        objects = orders.map(([k, a]) => [k, getObject(a)]);

    return data
        .slice()
        .sort((a, b) => {
            var v;
            objects.some(([k, o]) => v = o[a[k]] - o[b[k]]);
            return v;
        });
}

const
    palette = [{ color: 'Blue', brightness: 'Soft' }, { color: 'Blue', brightness: 'Medium' }, { color: 'Blue', brightness: 'Principal' }, { color: 'Magenta', brightness: 'Soft' }, { color: 'Magenta', brightness: 'Medium' }, { color: 'Magenta', brightness: 'Principal' }],
    colorOrder = ['Blue', 'Magenta'],
    brightnessOrder = ['Principal', 'Soft', 'Medium'],
    ordered = sortArrayByAnotherArrays(
        palette,
        [
            ['color', colorOrder],           // [key, values in order]
            ['brightness', brightnessOrder]
        ]
    );

console.log(ordered);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

通过colorOrder中的每个对象的颜色的索引差异进行排序,并以brightnessOrder中的每个对象的亮度的索引差异进行替换。

请记住,.sort是按位置排序的-sortedpalette是同一对象。如果您不想突变原始数组,请先对其进行克隆。

const palette=[{color:"Blue",brightness:"Soft"},{color:"Blue",brightness:"Medium"},{color:"Blue",brightness:"Principal"},{color:"Magenta",brightness:"Soft"},{color:"Magenta",brightness:"Medium"},{color:"Magenta",brightness:"Principal"}];

const colorOrder = ['Blue', 'Magenta'];
const brightnessOrder = ['Principal', 'Soft', 'Medium'];

const sorted = palette.sort((a, b) => (
  colorOrder.indexOf(a.color) - colorOrder.indexOf(b.color) ||
  brightnessOrder.indexOf(a.brightness) - brightnessOrder.indexOf(a.brightness)
));
console.log(sorted);

答案 2 :(得分:0)

如果结果取反,则可以尝试将排序条件取反

return order.indexOf(A) > order.indexOf(B) ? 1 : -1