Discussion Locked

Visual Basic Sorting

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I want to be able to sort strings alphabetically in descending order. I have saved these strings in a text file. The visual basic (6.0) program is to open the file, allow me to add entries and then save it upon exit in alphabetical order. I can openthe file, read it and save it fine, but I can't find a way to sort it. Any help would be appreciated :)

8 replies

by lo

Hi - Several ways you can do this. I took a quick look in VB6 for Sort but didn't find anything that looked like it would do the trick quickly.

You can create an hidden listbox, set the property for sorted (reverse it that is possible, been a while), load the file to the list box. Then take each entry (if reverse sort, 1st entry is 1st, if not, process backwards (last, last-1, etc.).

You can probably activate a DOS session and pass the file to SORT (from a DOS prompt HELP SORT for specifics on sort). Not sure about specifics of starting a DOS session from VB, but I know it's possible.

You can create an array, load the file to the array and sort (can sort as you load if you want).

There are probably other solutions. Check in VB Help, for other possibilities.

by donq

Just query your data:

SELECT: list the table.field compinations you want

FROM: name your data source

WHERE: list any constraines or links

ORDER BY: enter your sorting instructions

Look under SQL statements dor on-line help.

by peyison

The listbox method in Answer 1 is not bad if you have a small list. The best solution would be to write your own routine in VB. There are many different ways to sort a list, the best choice depends on the number of items in the list, and the condition of the list. In your case, since the list is sorted to begin with, before you insert new items, your best bet may be not a sorting solution at all. It may be best to insert the items in the already sorted list as you add them. So what you need is a search routine (so you can search for the position in the list to add the new item).

The fastest would be a binary search, but you could also just start from the top of the list and check every item until you find the position where the new item should go.

You will also need to know (or learn) a little about arrays and linked lists.

So - if you're interesed in learning a few things about VB, I'd recommend the binary search and insert method (or insertion sort). I'd be happy to help with this. If you're just looking for a quick hack to get the job done, use the sorted list box.

Hope this helps.

by peyison

By the way, I'm assuming that your using a text file, not a database file. If you're talking about a database file, answer 2 is right on.

This discussion is closed to new replies.