14.14 排序
按职工编号进行排序
1、实现功能
先声明
//按编号排序
void sort_Emp();
再实现,可以升序或降序
//按编号排序
void WorkerManager::sort_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请选择排序方式:" << endl;
cout << "1、按职工号升序" << endl;
cout << "2、按职工号降序:" << endl;
int select = 0;
cin >> select;
for (int i = 0; i < this->m_EmpNum; i++)
{
int minOrMax = i;
for (int j = i + 1; j < this->m_EmpNum; j++)
{
if (select == 1)
{
if (this->m_EmpArray[minOrMax]->m_Id > this->m_EmpArray[j]->m_Id)
{
minOrMax = j;
}
}
else
{
if (this->m_EmpArray[minOrMax]->m_Id < this->m_EmpArray[j]->m_Id)
{
minOrMax = j;
}
}
}
if (minOrMax != i)
{
Worker* temp = this->m_EmpArray[i];
this->m_EmpArray[i] = this->m_EmpArray[minOrMax];
this->m_EmpArray[minOrMax] = temp;
}
}
this->Save();
cout << "排序成功,结果显示如下:" << endl;
this->show_Emp();
}
}
2、在main里添加选择
case 6: //排序职工
wm.sort_Emp();
break;