Discussion Locked

Sort Order (ASP, Access)

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How is sorting applied? If I have a recordset and it's ORDERED BY a column, how is it ordered after that column? Here's what I mean:
Table = tblProducts
Fields = AutoID, Product, Color
Data:
1,Socks,red
2,Tutu,red
3,Socks,blue
4,Socks,green
5,Tutu,white

Now - if I order by PRODUCT (SELECT * FROM tblProducts ORDER BY Product), then I'll get:
Socks
Socks
Socks
Tutu
Tutu

But - HOW ARE THE SOCKS ORDERED? Does the Red sock come before the Blue sock? Are they ordered by the AutoID,by the color, randomly? Is it possible to define a "2nd" sort order, a 3rd, and so on...?

5 replies

by ann777

In most DBs, it will sort on the column specified, as rec'd. In your example, the first Sort List was by AutoID. If you select Order by product, the it will sort 1.3.4.2.5.

You also have the option to sort by more than one column, and a third and fourth... knowing of course, that the more sorts, the slower it'll run.

In your example (and using your syntax), you should be able to "order by product, color" (using your third field name) and you should then get 3.4.1.2.5

I hope that helps.... I have not done anything with Access, but Ashton-Tate's dBaseIII and dBaseIII+ was a lot of fun to program and work with creating databases (it was awhile ago that I did programming) and reports. I'm sure MS Access is capable of much more functionality and flexibility that dBaseIII+!

by madestroitsolutions

If you want to define more than one sorter, use the following syntax:

SELECT [.blah blah.] ORDER BY Fld1, Fld2,...Fldn

E.g:

SELECT * FROM tblProducts ORDER BY Product, Color

If you dont specify another sorter, there is NO WAY you can know how they will be sorted after the first one, since that depends in different things like indexes, etc, and the way this particular database handles records.

Since you seem to be fairly new to SQL and databases, may I recommend you buy a book about the subject, it will help you a lot with design. For example, you will learn of standard ways to name your tables and fields, so it becomes easier for you and others to understand it, etc, etc.

Good Luck! and don't forget to rate my answer

This discussion is closed to new replies.