组合排序

当一个对象存在多个属性,按照属性先后次序堆对象进行排序:(对象属性的数据类型不一致,有字符串,整型)

例如:

  对象一:"juv", 3, 3, -7 
对象"kad", 0, 3, -6
             对象三:"gui", 0, 9, -6 
对象"opq", 4, 3, -6 

对象"jpb", 0, 3, 0 

按照最后一列属性到第一列属性进行排序。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

struct Source{
	string name;
	int team_1;
	int team_2;
	int team_3;
	Source(string name, int t1, int t2, int t3) :name(name), team_1(t1), team_2(t2), team_3(t3){}
};

typedef vector<Source> vSource;

class MultiSort{
private:
	Source* _current;
public:
	vSource operator()(vSource& src)
	{
		//标准一排序:
		sort(src.begin(), src.end(), [&](Source& a, Source& b){ return a.team_3 > b.team_3; });
		//标准二排序:
		int count = 0, index = 0;
		for (int i = 0; i <= src.size(); ++i)
		{
			if (i == 0)
			{
				_current = &src[i];
			}
			else if (i < src.size() && _current->team_3 == src[i].team_3)
			{
				count++;
			}
			else
			{
				if (count > 1) //仅在个数大于1进行排序
				{
					sort(src.begin() + index, src.begin() + index + count, [&](Source& a, Source& b){ return a.team_2 > b.team_2; });
					Source *inner_current2 = &(src[index]);
					int inner_index2 = index, inner_count2 = 0;
					for (int ii = index; ii <= index + count; ++ii)
					{
						if (ii == index)
						{
							inner_current2 = &src[ii];
						}
						else if (ii < index + count && inner_current2->team_2 == src[ii].team_2)
						{
							inner_count2++;
						}
						else 
						{
							if (inner_count2 > 1) //仅在个数大于1进行排序
							{
								sort(src.begin() + inner_index2, src.begin() + inner_index2 + inner_count2, [&](Source& a, Source& b){ return a.team_1 > b.team_1; });
								Source* inner_current3 = &(src[inner_index2]);
								int inner_index3 = inner_index2, inner_count3 = 0;
								for (int iii = inner_index2; iii <= inner_index2 + inner_count2; ++iii)
								{
									if (iii == inner_index2)
									{
										inner_current3 = &src[iii];
									}
									else if (iii < inner_index2 + inner_count2 && inner_current3->team_1 == src[iii].team_1)
									{
										inner_count3++;
									}
									else
									{
										if (inner_count3 > 1) //仅在个数大于1进行排序
										{
											sort(src.begin() + inner_index3, src.begin() + inner_index3 + inner_count3, [&](Source& a, Source& b){ return a.name < b.name; });
										}
										inner_index3 = iii;
										inner_count3 = 1;
										if (iii < inner_index2 + inner_count3 + 1)
										{
											inner_current3 = &src[iii];
										}
									}
								}
							}
							
							inner_index2 = ii;
							inner_count2 = 1;
							if (ii < index + count + 1)
								inner_current2 = &src[ii];
						}
					}
				}
				index = i;
				count = 1;
				if (i < src.size())
					_current = &src[i];
			}
			
		}

		return std::move(src);
	}

	void show(const vSource& dst)
	{
		for (vSource::const_iterator itr = dst.begin(); itr != dst.end(); itr++)
		{
			cout << itr->name << " " << itr->team_1 << " " << itr->team_2 << " " << itr->team_3 << endl;;
		}
		cout << endl;
	}
}multiSort;


int main()
{
	string array_src[][4] = {
		{ "mid", "0", "3", "-6" },
		{ "juv", "3", "3", "-7" },
		{ "kad", "0", "3", "-6" },
		{ "gui", "0", "9", "-6" },
		{ "opq", "4", "3", "-6" },
		{ "jpb", "0", "3", "0" }
	};

	vSource src;

	for (int i = 0; i < 6; ++i)
	{
		string name = array_src[i][0];
		int t1 = stoi(array_src[i][1]);
		int t2 = stoi(array_src[i][2]);
		int t3 = stoi(array_src[i][3]);
		Source one(name, t1, t2, t3);
		src.push_back(one);
	}

	vSource result = multiSort(src);

	multiSort.show(result);

	return 0;
}

运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值