Skip to content

Commit 0cfaa56

Browse files
committed
feat: add time to sort and bubble sort comment
1 parent cd73aaf commit 0cfaa56

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/helpers/sort.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,18 @@ export const compareStrings = (a: string, b: string) => {
128128
// }
129129
};
130130

131-
export const sortProducts = (products: ProductTuple[], method: SortMethod) => {
131+
// Using Bubble Sort
132+
export const sortProducts = (products: ProductTuple[], method: SortMethod = "ascending") => {
132133
const result = filterProductsById(products);
133134

134135
for (let p = 0; p < products.length; p++) {
136+
// Outer pass
135137
// iterate over products
136138

137139
for (let i = 0; i < result.length; i++) {
140+
// Inner pass
138141
// sort each product by pick location
142+
139143
const currentProduct = result[i];
140144
const nextProduct = result[i + 1];
141145

src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ async function run(args: string[], options: CliOptions = {}) {
3939
checkColumns(columns);
4040
logger.info(`processing ${rows.length} rows`);
4141

42-
const sortedRows = sortProducts(rows, "ascending");
42+
const startedSort = Date.now();
43+
const sortedRows = sortProducts(rows);
44+
const finishedSort = Date.now();
4345

4446
const sortedProducts = [columns, ...sortedRows];
45-
logger.success(`sorted ${sortedProducts.length - 1} products`);
47+
const timeToSort = finishedSort - startedSort;
48+
logger.success(`sorted ${sortedRows.length} products in ${timeToSort}ms`);
4649

4750
createCsvFile(options.output, sortedProducts);
4851
}

0 commit comments

Comments
 (0)