Vector Sorting Using Bubble Sorting Algorithm

I’m new to programming and currently working on an app to assist with managing my home budget. All the data is stored in an XML file. To sort vectors by date, I’ve implemented a bubble sort algorithm. However, I’m encountering issues with incorrect results. Could someone please provide guidance on identifying the mistake?

#include <iostream>
#include <vector>
#include <string>
#include "Markup.h"

using namespace std;

class income{
    int userId;
    int amount;
    int itemId;
    string item;
    string date;
    public:
    void setUserId(int i) {userId = i;}
    void setAmount(int a) {amount = a;}
    void setItemId(int i ) {itemId = i;} // Corrected function name
    void setDate(string d) {date = d;}
    void setItem(string i) {item = i;}

    int getUserId()   {return userId;}
    int getAmount()   {return amount;}
    int getItemId()  {return itemId;}
    string getItem() {return item;}
    string getDate()  {return date;}
};

bool returnOlderDate(string date1,string date2) {

    int year1 = stoi(date1.substr(0,2));
    int month1 = stoi(date1.substr(3,2)); // Corrected substring indices
    int day1 = stoi(date1.substr(6,2));   // Corrected substring indices

    int year2 = stoi(date2.substr(0,2));
    int month2 = stoi(date2.substr(3,2)); // Corrected substring indices
    int day2 = stoi(date2.substr(6,2));   // Corrected substring indices

    if (year1 != year2) {
        return year1 < year2;
    }
    if (month1 != month2) {
        return month1 < month2;
    }
    return day1 < day2;
}
void sortVector(vector <income> &incomes) {

    int n = incomes.size();

    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            if (returnOlderDate(incomes[j].getDate(), incomes[j + 1].getDate())) {

                swap(incomes[j], incomes[j + 1]);
            } else {

            }
        }
    }
}

vector <income> LoadIncome() {

    CMarkup xml;

    vector <income> incomes;
    bool fileExists = xml.Load("income.xml");

    if(!fileExists) {
        cout << "Empty File" << endl;
        return incomes;
    }

    xml.ResetPos();
    xml.FindElem();
    xml.IntoElem();

    while(xml.FindElem()) {
        income newIncome; // Create a new income object
        xml.FindChildElem("userId");// userId
        newIncome.setUserId(stoi(xml.GetChildData())); // Use newIncome instead of income
        xml.FindChildElem("itemId");
        newIncome.setItemId(stoi(xml.GetChildData())); // Use newIncome instead of income
        xml.FindChildElem("amount");
        newIncome.setAmount(stoi(xml.GetChildData())); // Use newIncome instead of income
        xml.FindChildElem("item");
        newIncome.setItem(xml.GetChildData()); // Use newIncome instead of income
        xml.FindChildElem("date");
        newIncome.setDate(xml.GetChildData()); // Use newIncome instead of income
        incomes.push_back(newIncome); // Push newIncome into the vector
    }

    return incomes;
}

void showIncomes(vector <income>& incomes) { // Corrected function declaration

    for(auto i : incomes) {
        cout << "date: " << i.getDate() << endl;
    }
}

int main()
{
    vector <income> incomes; // Corrected vector declaration
    incomes = LoadIncome();

    sortVector(incomes);// Added to show sorted incomes
    showIncomes(incomes);



    return 0;
}

I tried to sort the vectors by date using bubble sort

1

Xml File

<Income>
<income>
<userId>1878039312</userId>
<incomeId>1877999744</incomeId>
<amount>23</amount>
<item>23</item>
<date>02-05-2024</date>
</income>
<income>
<userId>1878039312</userId>
<incomeId>1877999744</incomeId>
<amount>1243</amount>
<item>1243</item>
<date>01-06-2024</date>
</income>
<income>
<userId>1878039312</userId>
<incomeId>1877999744</incomeId>
<amount>1243</amount>
<item>1243</item>
<date>02-03-2023</date>
</income>
<income>
<userId>1878039312</userId>
<incomeId>1877999744</incomeId>
<amount>1243</amount>
<item>1243</item>
<date>28-05-2024</date>
</income>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật