Quote:
|
Originally Posted by reddogc22
...writing a program with C++ that will accept a series of long integers and print them out, first sorted numerically and then sorted lexically. A friend suggested I use strcmp()...
|
The most elegant solution would be using
qsort() which takes a function pointer to a user-defined function which handles comparisons, but newcomers may be confused by the syntax.
My suggestion would be:
- Create a structure containing two fields: one a long int & the other a string. When the value is read, store it, & store its string equivalent via functions like sprintf().
- Create an array of this structure & fill it with numeric values received as input along with its string equivalent.
- Implement whatever sorting algorithm you choose, however, implement it twice: once for numeric long int values, & a second time for strings -- here, you would use a function like strcmp() to compare one string to another.