I have a list that is retrieved with python from MS Access.
This list needs to be sorted alphabetically before being printed on the web browser.

Here is what I have and I need it sorted by the P_LastName.

dbc = odbc.odbc('Intra//')
csr = dbc.cursor()
q = "SELECT P_ID, P_FirstName, P_LastName, P_Company, P_Title FROM contacts WHERE P_Category = '%s'" % cat
csr.execute(q)
pdt = csr.fetchall()
csr.close()

I have been playing the with sort function but have been unsuccessful. Any help greatly appreciated.

Recommended Answers

All 2 Replies

Hi,
I am not sure how you access the P_LastName field in a member of q.
If it is something like q[0].P_LastName then use the key argument to sort like this:

q.sort(key = lambda x: x.P_LastName)

- Paddy.

It appears from your code that MS Access is SQL. If so, you can use ORDERBY. A Google for "access orderby" should get you started.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.