Qbasicnews.com

Full Version: numeric and alphabetical ordering
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i know this has been already asked by now; how do you order things numericly and/or alphabeticaly
Dude, seriously... The g0ddamn post is only 4 lines down in the forum. How lazy can you possibly get?

http://forum.qbasicnews.com/viewtopic.php?t=1937
Diroga,
Be a little more specific with your questions. You say "how do you order things...". What do you mean by "things". If these things are already on a file, your best bet is to use a sort utility assuming that the "things" are left justified if they're alphabetic, or right justified to a standard length if they're numeric. Do you get what I mean?

If these "things" are in memory, like numerics in an array, or alphabetic strings, left justified, in an array, then to order them you will want to use an internal sort algorithm, of which several good ones have been previously posted, like a nice bubble sort recently posted by Sumojo.

If you don't like or understand sort algorithms, you can always use an old "lookup low" technique, where you go through the entire list or array looking for the "lowest" value (assuming you want them in ascending order). When you find the lowest value, you use it for output (or whatever you're doing with these "things"), and then reset its array value to null or something so as not to process it again. Then you go through the array again for the next lowest. This is not a very good method, actually you're better off using an internal sort algorithm, which is easier to code.

Another clunky way is to write the data fields out to a random access file, and then reading them back in key order.

Assuming you have access to a sort uility, and have the data to be sorted in memory, another lazy way is to write it all out to a text file, formatted (left justified for alpha and right justified for numerics). Then close the file, and shell to the sort utility (with the proper parameters of course), then read the sorted file into your program to do what you want.

For the moment I can't think of other ways to avoid using an internal sort algorithm.

I suggest you find the code for an algorithm that you like, the simpler the better, since speed is not an issue for small amounts of data that can fit in memory. Then do some heavy testing of this code until you thoroughly understand and master it. Now you have a reliable sort routine that you can use for years to come.
*****
Diroga,
Just for curiosity, what sort method did you end up using?
*****
i never got around to. i takes me a long time to figure this stuff out.
<lame>
*BANG*
</lame>
Diroga,
That's ok, but keep this sorting issue on a back burner as something that you defintely want to learn sooner or later.
*****