* Helper function to sort results by key, then index, then id
(results: Array<any>)
| 450 | * Helper function to sort results by key, then index, then id |
| 451 | */ |
| 452 | function sortByKeyIndexAndId(results: Array<any>) { |
| 453 | return [...results].sort( |
| 454 | ( |
| 455 | [[aKey, [aValue, aIndex]], _aMultiplicity], |
| 456 | [[bKey, [bValue, bIndex]], _bMultiplicity], |
| 457 | ) => { |
| 458 | // First sort by key |
| 459 | if (aKey !== bKey) { |
| 460 | return aKey < bKey ? -1 : 1 |
| 461 | } |
| 462 | // Then by index |
| 463 | if (aIndex !== bIndex) { |
| 464 | return aIndex - bIndex |
| 465 | } |
| 466 | // Then by id if indices are the same |
| 467 | return aValue.id - bValue.id |
| 468 | }, |
| 469 | ) |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Helper function to sort results by multiplicity, then index, then id |