Sorting numbers with strings
Author |
Message |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Tue Mar 22, 2005 6:33 pm Post subject: Sorting numbers with strings |
|
|
err. I came across this, Ok, suppose i have something like this
Height Name
123 Me
234 You
213 Someone
789 Friend
Now if I want to sort them, I can sort the numbers for example:
123
213
234
789
but the problem asks for the names with them:
123 Me
213 Someone
234 You
789 Friend
Can some1 help? ![Crying or Very sad Crying or Very sad](http://compsci.ca/v3/images/smiles/icon_cry.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Tue Mar 22, 2005 9:36 pm Post subject: (No subject) |
|
|
i beleive you can just sort the list like any other string and it will come out in ascending order... |
|
|
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Wed Mar 23, 2005 6:44 am Post subject: (No subject) |
|
|
if all else fails, just put the two in two different arrays, and match em up. |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Wed Mar 23, 2005 12:32 pm Post subject: (No subject) |
|
|
I thought of that but how do i match em up. I mean i know how to match :
code: |
for x : 1..upper(one)
for s : 1..upper(two)
if one(s) = two(s) then
do blah
end if
end for
end for |
but one is int and the other is string. any example? |
|
|
|
|
![](images/spacer.gif) |
jamonathin
![](http://compsci.ca/v3/uploads/user_avatars/57683465145f851a43dd9a.gif)
|
Posted: Wed Mar 23, 2005 3:43 pm Post subject: (No subject) |
|
|
After you're done sorting the numbers, just display them side by side . .
code: |
var score : array 1 .. 4 of int := init (123, 234, 213, 789)
var name : array 1 .. 4 of string := init ("Me", "You", "Someone", "Friend")
for i : 1 .. 4
put score (i), " ", name (i)
end for
|
|
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Wed Mar 23, 2005 4:19 pm Post subject: (No subject) |
|
|
you can sort *but* you have to ensure that all the numbers are of the same length by adding leading zeros. otherwise youre gonna have to split the array into 2 different ones and sort the numbers... when you swap 2 numebrs, swap the names aswell, then add the 2 arrays back together... |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Fri Mar 25, 2005 8:35 am Post subject: (No subject) |
|
|
i c what you mean, i will try to take it on from here. Thanks ![Very Happy Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|