Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

How to sort a char array that contains the date in string form from largest to smallest? any method will be great, Thanks

OpenStudy (anonymous):

Well, depending on how large your array is, you might want to look into different sorting algorithms. Also.. do you already have some sort of a compare function? To decide, what date is "bigger/smaller" being "later/earlier" ? What language are we talking about ?

OpenStudy (anonymous):

Depends on the language you're coding in. Most languages have sorting methods.

OpenStudy (anonymous):

It depends on the date format far more than on the language. Usually you would need to extract the different parts of the date, starting with the year, then month, etc., and use those as the basis for comparing two dates. Or you could just convert all the dates into ISO format (year-month-day hour minute second) and just use a regular String sort.

OpenStudy (anonymous):

language: c++ date format: cstring ex. //my method, however it is no good char date[11]; //user enter date in format: 01/01/11 for loop //I converted each char to ASCII values, store to a total variable. { //codes } //I used selection sort in the for loop, and compared the total value for each date, however by adding the ASCII value for each char does not work according to the order. ::to NCZEMPIN, Could you show me some pseudo code for the method of getting years, month, date. P.S Im guessing your testing year, then moth, then date

OpenStudy (anonymous):

the format the user enters in is ambiguous. I assume the US convention month day year I would simply turn "01/01/11" into "2011-01-01", put each string into a vector and use the C++ standard library to sort. However, I'm not sure if perhaps I've misunderstood or if you have initially phrased the problem ambiguously. I was assuming you have _several_ strings and you want to sort them by date. What you showed was _one_ string where the individual _characters_ are meant to be sorted. Can you please clarify that first?

OpenStudy (anonymous):

date has to be in month-date-year, also there r 20 dates, each date is stored under a struct. But you can just use char date[20][11], and sotre the dates from old to new.

OpenStudy (anonymous):

what about a year like 99? Is that bigger or smaller than 11? What about 50?

OpenStudy (anonymous):

"each date is stored under a struct" Is this a requirement that you didn't previously tell us about (you said "any method will be great", does that still hold?) or just how you're implementing it right now? In what format do the dates have to be after the sort?

OpenStudy (anonymous):

Im confusing my self, im going to attach part of a file that has the sort for the date (it output ok, except not in the right order..

OpenStudy (anonymous):

what is the story behind using so many separate for loops?

OpenStudy (anonymous):

You said "language: C++", why are you using none of the C++ features, especially not std:strings and none of the standard library functions? Was it part of the assignment?

OpenStudy (anonymous):

yes, we never learn to use those features. The teacher is horrible. It is a project, the basic mission is a list of 20 books, u can add/delete if possible, buy books calculating prices etc, and show reports. With the so many loops, I was coding as I was thinking, so i didnt compress them together. Pretty confusing huh? sry. Ill explain...... Those trash arrays are only used so it dont alter the real/original array. We do not want to sort the original array, instead we need to get the indexes of them from oldest to youngest. So i used a trash to store original array and sort them in order, then used another trash2 to compare trash and trash2, and get the indexes. then use the indexes to get the value from the original array. So pretty much indexes wont be like 0,1,2,3,4,5,, etc, instead it would be jumping around depend on the original array data

OpenStudy (anonymous):

You are right, your code is confusing. I guess since your teacher hasn't mentioned the C++ parts, it wouldn't make any sense to use them all of a sudden. I suggest you first write a separate program that simply lets you sort integers. Once you have that, find how to extract the year, month and day from the strings (I hope you can assume that the user enters valid dates; otherwise you will have to check input validity too) And then, where normally in comparing two integers for sorting, just use a more complicated set of conditions that tell you which of two dates is earlier, i. e.: if the year is smaller or larger -> decision is done else if the month is smaller or bigger [but the year is the same; this follows from the previous] -> decision is done etc. As for the sorting algorithm, I assume you don't have to do anything fancy, so a simple insertion sort will do (check online, or perhaps ask another question)

OpenStudy (anonymous):

so, the teacher is teach us the old code, which is c ? not c++? and Yes, Ill try the comparison, ty

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!