javascript - 排序功能并忘记以前的结果
问题描述
我有一系列不同类型的行李。
我正在尝试将其排序为:
- 包括免费随身行李
- 继续
- 包括免费行李
- 行李
- 其他行李类型,例如机舱内的宠物
它在 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);
解决方案
推荐阅读
- android - 如何同时使用 AppCompatActivity 和 RecyclerView?
- rust - 如何初始化数组并保存对其项目的引用?
- reactjs - 修复由 Document 抛出的 next.js 的 _document.tsx 中的 ESLint 警告
和 ctx.renderPage = () 使用 TypeScript 时 - javascript - The difference between callback and just a function being called inside of another function
- angular - Firestore first()).toPromise() always gives old value even I have re-run with new changes
- java - 无法让第二个视图列表将列表作为参数
- c# - Show DIV on different part of page
- python - 根据另一列的值一次创建新列
- sqlalchemy - 在多态标识中包含棉花糖模式中的子字段
- json - 如何从 json 文件中读取并在 cli 中创建环境变量?