Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Algorithms
Confused | :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();
	   ave_day_data++;				 		
	}
	system("pause");
	total_day_wind = 0.0;
	ave_day_data = 0;
Posted 16-Jun-10 7:00am
Edited 16-Jun-10 7:03am
Moak11.4K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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() );
 
Cheers,
 
Ash
 
Edited as yet again I forgot to escape angle brackets
  Permalink  
v2
Comments
valtron78 at 16-Jun-10 22:40pm
   
Does it mean I do a sort first before I start counting the total?
Aescleal at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Aescleal at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 16 Jun 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100