Click here to Skip to main content
11,805,604 members (54,454 online)
Rate this: bad
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();
	total_day_wind = 0.0;
	ave_day_data = 0;
Posted 16-Jun-10 6:00am
Edited 16-Jun-10 6:03am
Rate this: bad
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() );



Edited as yet again I forgot to escape angle brackets
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
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.
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)

  Print Answers RSS
0 OriginalGriff 300
1 Richard Deeming 225
2 Sergey Alexandrovich Kryukov 170
3 ppolymorphe 125
4 Abhinav S 118
0 OriginalGriff 3,300
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 16 Jun 2010
Copyright © CodeProject, 1999-2015
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