[node] factorize sortByPrice
This commit is contained in:
parent
09537408a5
commit
3306159369
3 changed files with 21 additions and 9 deletions
|
@ -21,6 +21,10 @@ type Point = {
|
|||
y: number;
|
||||
};
|
||||
|
||||
type Price = {
|
||||
price: number;
|
||||
};
|
||||
|
||||
// cargo is a ship.cargo object, want is an optional symbol
|
||||
export function categorizeCargo(cargo: Cargo, want?: string): CategorizedCargo {
|
||||
const wanted = cargo.inventory.filter(i => i.symbol === want || i.symbol === 'ANTIMATTER');
|
||||
|
@ -60,6 +64,17 @@ export function sortByDistanceFrom<T extends Point>(a: Point, points: Array<T>):
|
|||
return result;
|
||||
}
|
||||
|
||||
export function sortByPrice<T extends Price>(data: Array<T>): void {
|
||||
data.sort(function(a, b) {
|
||||
if (a.price < b.price) {
|
||||
return -1;
|
||||
} else if (a.price > b.price) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
type Step = {waypoint: Waypoint, prev: string, fuel: number, total: number};
|
||||
type ShortestPath = Array<{symbol: string, fuel: number}>;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue