@@ -40,29 +40,55 @@ export const sortProducts = (products: ProductTuple[]) => {
4040
4141type SortMethod = "ascending" | "descending" ;
4242
43+ const filterProductsById = ( products : ProductTuple [ ] ) => {
44+ const productsMap = new Map < string , ProductTuple > ( ) ;
45+
46+ for ( let p = 0 ; p < products . length ; p ++ ) {
47+ const product = products [ p ] ;
48+ const [ id ] = product ;
49+
50+ if ( productsMap . has ( id ) ) {
51+ const existingProduct = productsMap . get ( id ) ;
52+
53+ if ( existingProduct ?. length === 3 ) {
54+ const [ , existingQty ] = existingProduct ;
55+ const [ , qty ] = product ;
56+ existingProduct [ 1 ] = String ( Number ( existingQty ) + Number ( qty ) ) ;
57+ productsMap . set ( id , existingProduct ) ;
58+ }
59+ continue ;
60+ }
61+ productsMap . set ( id , product ) ;
62+ }
63+
64+ return [ ...productsMap . values ( ) ] ;
65+ } ;
66+
4367export const newSortProducts = ( products : ProductTuple [ ] , method : SortMethod ) => {
44- const result = [ ...products ] ;
45- console . log ( "method" , method ) ;
68+ const filtered = filterProductsById ( products ) ;
69+ const result = [ ...filtered ] ;
70+
71+ for ( let p = 0 ; p < products . length ; p ++ ) {
72+ // iterate over products and check for uniques
73+ const product = products [ p ] ;
74+ const [ id ] = product ;
4675
47- for ( let p = 0 ; p < result . length ; p ++ ) {
48- // iterate over products
49- console . log ( "product" , result [ p ] ) ;
5076 for ( let i = 0 ; i < result . length ; i ++ ) {
5177 // sort each product by pick location
52- // console.log("i", i);
5378 const currentProduct = result [ i ] ;
5479 const nextProduct = result [ i + 1 ] ;
5580 console . log ( currentProduct , nextProduct ) ;
81+
5682 if ( ! nextProduct ) {
5783 break ;
5884 }
85+
5986 const [ , , currentPickLocation ] = currentProduct ;
6087 const [ , , nextPickLocation ] = nextProduct ;
6188 const [ currentBay , currentShelf ] = currentPickLocation . split ( " " ) ;
6289 const [ nextBay , nextShelf ] = nextPickLocation . split ( " " ) ;
6390
6491 if ( currentBay === nextBay ) {
65- // console.log("same bay");
6692 if ( Number ( currentShelf ) > Number ( nextShelf ) ) {
6793 swap ( result , i , i + 1 ) ;
6894 }
@@ -71,6 +97,14 @@ export const newSortProducts = (products: ProductTuple[], method: SortMethod) =>
7197 if ( currentBay > nextBay ) {
7298 swap ( result , i , i + 1 ) ;
7399 }
100+
101+ if ( currentBay < nextBay ) {
102+ continue ;
103+ }
104+
105+ // if currentBay is more than one letter eg AZ not A
106+ // then sort by the first letter
107+ // then sort by the second letter
74108 }
75109 }
76110
0 commit comments