129 lines
4.3 KiB
JavaScript
129 lines
4.3 KiB
JavaScript
// 测试新的排序逻辑
|
|
const members = [{"id":"4661dd7b","name":"唐治明","birthYear":1960,"gender":"男","generation":"父亲","fatherId":"f24a95c7"},{"id":"d4498bfb","name":"舒邦莲","birthYear":1960,"gender":"女","generation":"父亲"},{"id":"f24a95c7","name":"唐有奎","birthYear":1900,"gender":"男","generation":"祖父"},{"id":"1be537aa","name":"唐治安","birthYear":1960,"gender":"男","generation":"父亲","fatherId":"f24a95c7"},{"id":"e6d9c5b3","name":"万华群","birthYear":1960,"gender":"女","generation":"父亲"},{"id":"4b78f55d","name":"唐治芳","birthYear":1960,"gender":"女","generation":"父亲","fatherId":"f24a95c7"},{"id":"a7bcc3b5","name":"邓能春","birthYear":1960,"gender":"男","generation":"父亲"}];
|
|
|
|
const map = {};
|
|
members.forEach(m => map[m.id] = m);
|
|
|
|
// 构建配偶关系
|
|
const families = {
|
|
'4661dd7b|d4498bfb': { fatherId: '4661dd7b', motherId: 'd4498bfb' },
|
|
'1be537aa|e6d9c5b3': { fatherId: '1be537aa', motherId: 'e6d9c5b3' },
|
|
'a7bcc3b5|4b78f55d': { fatherId: 'a7bcc3b5', motherId: '4b78f55d' }
|
|
};
|
|
|
|
const familyKeyOfChild = {
|
|
'4661dd7b': 'f24a95c7|',
|
|
'1be537aa': 'f24a95c7|',
|
|
'4b78f55d': 'f24a95c7|'
|
|
};
|
|
|
|
const prevOrderIndex = { 'f24a95c7': 0 };
|
|
|
|
// 识别配偶关系
|
|
const spouseOf = {};
|
|
Object.keys(families).forEach(key => {
|
|
const fam = families[key];
|
|
spouseOf[fam.fatherId] = fam.motherId;
|
|
spouseOf[fam.motherId] = fam.fatherId;
|
|
});
|
|
|
|
console.log('配偶关系:', spouseOf);
|
|
|
|
// 按血缘分组(兄弟姐妹)
|
|
const siblingGroups = {};
|
|
members.forEach(m => {
|
|
const key = familyKeyOfChild[m.id] || `solo:${m.id}`;
|
|
if (!siblingGroups[key]) siblingGroups[key] = [];
|
|
siblingGroups[key].push(m);
|
|
});
|
|
|
|
console.log('\n兄弟姐妹分组:');
|
|
Object.keys(siblingGroups).forEach(key => {
|
|
console.log(` ${key}: ${siblingGroups[key].map(m => m.name).join(', ')}`);
|
|
});
|
|
|
|
// 构建家庭单元(每个兄弟姐妹 + 配偶)
|
|
const familyUnits = [];
|
|
Object.keys(siblingGroups).forEach(groupKey => {
|
|
const siblings = siblingGroups[groupKey];
|
|
const processed = new Set();
|
|
|
|
siblings.forEach(person => {
|
|
if (processed.has(person.id)) return;
|
|
processed.add(person.id);
|
|
|
|
const spouseId = spouseOf[person.id];
|
|
if (spouseId && map[spouseId] && !processed.has(spouseId)) {
|
|
processed.add(spouseId);
|
|
const spouse = map[spouseId];
|
|
familyUnits.push({
|
|
type: 'couple',
|
|
groupKey,
|
|
members: person.gender === '男' ? [person, spouse] : [spouse, person]
|
|
});
|
|
} else {
|
|
familyUnits.push({
|
|
type: 'single',
|
|
groupKey,
|
|
members: [person]
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
console.log('\n家庭单元:');
|
|
familyUnits.forEach((unit, i) => {
|
|
const names = unit.members.map(m => m.name).join(' + ');
|
|
console.log(` Unit ${i} (${unit.type}, ${unit.groupKey}): ${names}`);
|
|
});
|
|
|
|
// 对家庭单元排序
|
|
function getUnitWeight(unit) {
|
|
const key = unit.groupKey;
|
|
if (key.startsWith('solo:')) {
|
|
return { parentIdx: null, birthYear: unit.members[0].birthYear };
|
|
}
|
|
|
|
const parts = key.split('|');
|
|
const fId = parts[0] || null;
|
|
const indices = [];
|
|
if (fId && prevOrderIndex[fId] !== undefined) indices.push(prevOrderIndex[fId]);
|
|
const parentIdx = indices.length > 0 ? Math.min(...indices) : null;
|
|
const birthYear = Math.max(...unit.members.map(m => m.birthYear));
|
|
|
|
return { parentIdx, birthYear };
|
|
}
|
|
|
|
familyUnits.sort((a, b) => {
|
|
const wa = getUnitWeight(a);
|
|
const wb = getUnitWeight(b);
|
|
|
|
if (wa.parentIdx !== null && wb.parentIdx !== null) {
|
|
if (wa.parentIdx !== wb.parentIdx) return wa.parentIdx - wb.parentIdx;
|
|
} else if (wa.parentIdx !== null) {
|
|
return -1;
|
|
} else if (wb.parentIdx !== null) {
|
|
return 1;
|
|
}
|
|
|
|
if (wa.birthYear !== wb.birthYear) return wb.birthYear - wa.birthYear;
|
|
return 0;
|
|
});
|
|
|
|
console.log('\n排序后的家庭单元:');
|
|
familyUnits.forEach((unit, i) => {
|
|
const names = unit.members.map(m => m.name).join(' + ');
|
|
const weight = getUnitWeight(unit);
|
|
console.log(` ${i}. ${names} (parentIdx=${weight.parentIdx}, birthYear=${weight.birthYear})`);
|
|
});
|
|
|
|
// 展开为最终结果
|
|
const result = [];
|
|
familyUnits.forEach(unit => result.push(...unit.members));
|
|
|
|
console.log('\n最终排序结果:');
|
|
console.log(result.map(m => m.name).join(', '));
|
|
|
|
console.log('\n期望结果:');
|
|
console.log('唐治安, 万华群, 唐治芳, 邓能春, 唐治明, 舒邦莲');
|