0

I have array of baggage with different types.

I am trying to sort it like:

  • included free carryon
  • carryon
  • included free baggage
  • baggage
  • other baggage types like pet in cabin

It works fine in in Chrome, but in Firefox it shows in wrong order.

Can you explain why?

What are the best practices for this type of sorting?

Splitting conditions to few small functions isn't working because it ruins previous order.

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);
cezar
  • 11,616
  • 6
  • 48
  • 84

0 Answers0