首页 > 解决方案 > 排序功能并忘记以前的结果

问题描述

我有一系列不同类型的行李。

我正在尝试将其排序为:

它在 Chrome 中运行良好,但在 Firefox 中显示顺序错误。

你能解释一下为什么吗?

这种排序的最佳实践是什么?

将条件拆分为几个小功能是行不通的,因为它会破坏以前的顺序。

const baggage = [
    {
        id: '0',
        segmentsId: [['0']],
        includedSegments: ['0'],
        type: 'CarryOn',
        name: 'Ручная кладь',
        included: 1,
        price: null,
        value: { amount: 1, measurement: 'kg' },
        size: ''
    },
    {
        id: '0',
        segmentsId: [['0']],
        includedSegments: ['0'],
        type: 'CheckedBaggage',
        name: 'Багаж',
        included: 1,
        price: null,
        value: { amount: 1, measurement: 'kg' },
        size: ''
    },
    {
        id: '16',
        segmentsId: [['0']],
        type: 'CheckedBaggage',
        selectedBaggage: {},
        value: { amount: 15, measurement: 'kg' },
        size: 'до 203 см в сумме',
        included: 0,
        price: { amount: 1299, currency: 'RUB', __typename: 'Money' },
        name: 'Чемодан',
        description: 'размеры до 203 см в сумме трех измерений'
    },
    {
        id: '18',
        segmentsId: [['0']],
        type: 'CheckedBaggage',
        selectedBaggage: {},
        value: { amount: 20, measurement: 'kg' },
        size: 'до 203 см в сумме',
        included: 0,
        price: { amount: 1699, currency: 'RUB', __typename: 'Money' },
        name: 'Чемодан',
        description: 'размеры до 203 см в сумме трех измерений'
    },
    {
        id: '19',
        segmentsId: [['0']],
        type: 'CheckedBaggage',
        selectedBaggage: {},
        value: { amount: 25, measurement: 'kg' },
        size: 'до 203 см в сумме',
        included: 0,
        price: { amount: 2199, currency: 'RUB', __typename: 'Money' },
        name: 'Чемодан',
        description: 'размеры до 203 см в сумме трех измерений'
    },
    {
        id: '26',
        segmentsId: [['0']],
        type: 'CarryOn',
        selectedBaggage: {},
        value: { amount: 10, measurement: 'kg' },
        size: 'до 115 см в сумме',
        included: 0,
        price: { amount: 1500, currency: 'RUB', __typename: 'Money' },
        name: 'Ручная кладь',
        description: 'размеры до 115 см в сумме трех измерений'
    },
    {
        id: '37',
        segmentsId: [['0']],
        selectedBaggage: {},
        value: { amount: null, measurement: 'kg' },
        included: 0,
        price: { amount: 2250, currency: 'RUB', __typename: 'Money' },
        name: 'PET IN CABIN',
        description: null
    }
];


baggage.sort((a, b) => (a.type === BaggageType.CarryOn && b.type !== BaggageType.CarryOn ? -1 : 1));

// i'm also try this two functions
    const includedFirstSort = (a: Baggage, b: Baggage) => {
        // console.log(b.included - a.included);
        // debugger;
        if (a.included === b.included) {
            return 0;
        }

        return b.included - a.included;
    };

    const carryOnFirstSort = (a: Baggage, b: Baggage) => {
        if (a.type === BaggageType.CarryOn && b.type !== BaggageType.CarryOn) {
            return -1;
        } else {
            return 1;
        }
    };

    baggage.sort(includedFirstSort);
    baggage.sort(carryOnFirstSort);

标签: javascriptsorting

解决方案


推荐阅读