bubble sort for sorting the item array based on name of item, vendor name of item, and date of purchase

  bubbleSort: function (arr, property, value, order) {
                try {
                    var len = arr.length;
                    for (var i = 0; i < len; i++) {
                        for (var j = 0; j < len - 1; j++) {
                            if (value != undefined && value != '' && property === 'purchaseObject') {
                                var vendorA = arr[j].purchaseObject ? arr[j].purchaseObject.vendor : '';
                                var vendorB = arr[j + 1].purchaseObject ? arr[j + 1].purchaseObject.vendor : '';
                                // Check if vendors are empty
                                if (order === 'A') {
                                    if (!vendorA && !vendorB) {
                                        continue; // No change in order if both are empty
                                    } else if (!vendorA) {
                                        // Move items with empty vendor to the end
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    } else if (!vendorB) {
                                        // Keep items with empty vendor at the end
                                        continue;
                                    } else if (vendorA.toUpperCase() > vendorB.toUpperCase()) {
                                        // Compare vendors and swap if necessary
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                }
                                else{
                                    if (!vendorA && !vendorB) {
                                        continue; // No change in order if both are empty
                                    } else if (!vendorA) {
                                        // Move items with empty vendor to the end
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    } else if (!vendorB) {
                                        // Keep items with empty vendor at the end
                                        continue;
                                    } else if (vendorA.toUpperCase() < vendorB.toUpperCase()) {
                                        // Compare vendors and swap if necessary
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                }

                            } else if (property === 'PurchaseFirst') {
                                var a = arr[j][property];
                                var b = arr[j + 1][property];
                                // Check if values are numeric, convert to number for comparison
                                if (!isNaN(a) && !isNaN(b)) {
                                    a = Number(a);
                                    b = Number(b);
                                }
                                if (order === 'A') {
                                    if (a > b) {
                                        // Swap objects
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                } else {
                                    if (a < b) {
                                        // Swap objects
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                }

                            }
                            else {
                                if (order === 'A') {
                                    if (arr[j][property].toUpperCase() > arr[j + 1][property].toUpperCase()) {
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                } else {
                                    if (arr[j][property].toUpperCase() < arr[j + 1][property].toUpperCase()) {
                                        var temp = arr[j];
                                        arr[j] = arr[j + 1];
                                        arr[j + 1] = temp;
                                    }
                                }

                            }

                        }
                    }
                    return arr;
                } catch (error) {
                    log.debug('error@bubbleSort', error)
                }

            },

Leave a comment

Your email address will not be published. Required fields are marked *