Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

List not sorted well

$
0
0

Hi,
Here are the codes

struct nameval
{
	std::string fld_nm;
	std::wstring fld_nm_uni;
	int fld_len;
	float fld_val;
};
nameval binrec;

bool LessComp(const nameval& a1, const nameval& a2)
{
  if(strcmp(a1.fld_nm.c_str(), a2.fld_nm.c_str()) < 0) return true;
  if(strcmp(a1.fld_nm.c_str(), a2.fld_nm.c_str()) > 0) return false;
  if(a1.fld_val < a2.fld_val) return true;
  return false;
}

int main()
{
    std::vector<nameval> records;
    int cnt;
	for (cnt=0;cnt<100;cnt++)
	{
		nameval val;
		int j;
		val.fld_nm.resize(21);
		for (j=0;j<20;j++)
		{
			val.fld_nm.push_back(rand () % 58 + 64);
		}
		val.fld_val=cnt;
		records.push_back(val);
	}
    std::ofstream ostrm("c:\\dp4\\flout.bin", std::ios::binary | std::ios::out );
	if (ostrm.is_open())
	{
		for (size_t i = 0; i < records.size(); i++)
		{
			ostrm.write((char*)&records[i], sizeof(nameval));
		}
	}

	std::sort(records.begin(), records.end(), LessComp);
	std::ofstream ostrm2("c:\\dp4\\flout.ord", std::ios::out );
	if (ostrm2.is_open())
	{
		for (size_t i = 0; i < records.size(); i++)
		{
			std::cout << records[i].fld_nm;
			std::cout << endl;
		}
	}
	...

I try to sort the list above but it seems the output list has not been sorted properly below. why?
         ...
         @lBe_tNTIY^CNO@neyyO
         U_fGEjFgllKiKjmrbZQ@
         IlafZfIxJnDLJyZT@GOF
         StZKmRWhK@KXkW^h]\mn
         U@dZbru\RH]ye]YTwMoY
         kgxuUMeXeOtfFFaeDNMT
         is\[X\elWdrBgWoryiAp


Many Thanks & Best Regards, Hua Min


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>