Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
:confused:Below is my code for a mini project, I would like to know how to do an ascending sort to sort total_day_wind in ascending order.

cout << "Please enter a specific value : ";
cin >> wind_value;

	for(i=0; i<dataSize; i++)
	   if(data[i].date().day() != currentDay)
	      total_day_wind /= ave_day_data;
	      if (wind_value <= total_day_wind)
	cout << "Average WindSpeed equal or above " << wind_value << " at Month " << data[i].date().month() << " on Day " << data[i].date().day()  << " is " << total_day_wind << " knots " << endl;							
		currentDay = data[i].date().day();
		total_day_wind = 0.0;
		ave_day_data = 0;			
	   total_day_wind += data[i].windspeed();
	total_day_wind = 0.0;
	ave_day_data = 0;
Updated 16-Jun-10 6:03am

total_day_wind looks like either a float or double to me. It's a bit trivial to sort a single value...

If you've got a collection of wind speeds somewhere and you want to sort them into ascending order then use std::sort:

std::vector<double> wind_speeds;

// Much clever code to populate wind_speeds

std::sort( wind_speeds.begin(), wind_speeds.end() );



Edited as yet again I forgot to escape angle brackets
Share this answer
valtron78 16-Jun-10 22:40pm    
Does it mean I do a sort first before I start counting the total?
Aescleal 17-Jun-10 5:47am    
Does it matter? I have no idea what your code's trying to achieve so I'm afraid I can't answer that question.
The typical way to do this is by using the C++ runtime qsort function.

Another quickie way is to populate a hidden, sorted listbox (the item data is the index into your array). After all data strings have been added, you have a sorted list.
Share this answer
Aescleal 16-Jun-10 14:40pm    
Reason for my vote of 1
Yeuch. If you're using C you don't touch qsort with a bargepole. std::sort will give at least as good performance as qsort and is usually better (std::sort can inline the comparison which in qsort has to be through a function pointer).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900