11-07-2004, 06:44 AM
In the Newbie Help section Licentia recently posted a problem regarding sorting in descending order. This reminded me of similar problem about sorting 2 different keys where one must be ascending and the other descending.
If you're not using a Quick sort, you could do two sorts on the data, one for each key. But that's kinda like cheating.
So here's the challenge.
GIVEN:
* Data records containing a LAST NAME string, left justified, in positions 1 to 10 of each record.
* Also in each record we have an AGE string, right justified, in positions 11 to 12.
* There should be at least 10 records, and at least 2 or more should have the same NAME with different AGES.
PROBLEM:
* Write any sort routine you like to sort, in one pass, the NAME fields in ascending order, and to also sort the AGE fields in descending order.
Example: If there are 3 SMITH records with ages 10, 15 and 20, then the sorted output for these should show:
SMITH 20
SMITH 15
SMITH 10
Get it?
HINT: The solution is not in having a tricky sort routine, but in setting up your sort key properly.
*****
If you're not using a Quick sort, you could do two sorts on the data, one for each key. But that's kinda like cheating.
So here's the challenge.
GIVEN:
* Data records containing a LAST NAME string, left justified, in positions 1 to 10 of each record.
* Also in each record we have an AGE string, right justified, in positions 11 to 12.
* There should be at least 10 records, and at least 2 or more should have the same NAME with different AGES.
PROBLEM:
* Write any sort routine you like to sort, in one pass, the NAME fields in ascending order, and to also sort the AGE fields in descending order.
Example: If there are 3 SMITH records with ages 10, 15 and 20, then the sorted output for these should show:
SMITH 20
SMITH 15
SMITH 10
Get it?
HINT: The solution is not in having a tricky sort routine, but in setting up your sort key properly.
*****