Python Forum
How to perform DESC table sort on dates stored as TEXT type.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to perform DESC table sort on dates stored as TEXT type.
#1
SQLite does not offer a Date type in the drop down that i could find. I think i read to use TEXT type for dates. When i return a query using DESC for this field it almost sorts correctly but for instance, 4-5-2022 is listed before 4-11-2022.
Will i have to do the sorting of the returned list outside the query?
If so is there a standard way of doing this like converting the string to a datetime and then sorting?
Reply
#2
What do you mean by "SQLite does not offer a Date type in the drop down"? What "drop down"? What sqlite tool am I missing out on?

There is not a datetime table type, so you have choices of saving date as a string, float or int. If a string I suggest using a format that is sortable: (YYYY-MM-DD).

If you made a bad choice on the date string format you can use a key when sorting. You could convert dates to a datetime object, or you could make a tuple(year, month, day), or you could covert to a date string amenable to sorting. This code converts the date string to a tuple(year, month, day) of ints.
dates = ["6-11-1992", "6-5-1992", "6-7-1962"]

def date_sort_key(date_str):
    m, d, y = map(int, date_str.split('-'))
    return (y, m, d)

dates.sort(key=date_sort_key)

print(dates)
Output:
['6-7-1962', '6-5-1992', '6-11-1992']
Reply
#3
I realize now what you are saying about date format matters when sorting- yyyy-mm-dd will always be increasing in value for future dates where mm-dd-yyyy can be all over the place.
I think i will use above strategy with key to just sort returned list from query before displaying.

Thank you.

I use DBBrowser for SQLite and it has a dropdown when creating the field that provides options: Text,Integer,BLOB,Real,Numeric. Sorry for not presenting that point better.
Reply
#4
How do you take a query return that has multiple fields and sort on the one date field and rearrange the rows returned accordingly?
Reply
#5
DBBrowser is a nice tool. I don't do a lot of database work, but that will come in handy. Thanks!

I searched around a bit and I don't think you can ask the query to sort the results using a sorting key. That makes sense since the sorting key code is in your program, not in the database. You'll have to retrieve all the results and sort them in a list. Hope that is ok. I used DBBrowser to make a little database with my dates in it (table=date, month, day, year)
import sqlite3 as sql

def sort_by_date_str(x):
    m, d, y = map(int, x[0].split('-'))
    return (y, m, d)

connection = sql.connect("testdb.db")
cursor = connection.cursor()
results = cursor.execute("SELECT * FROM dates").fetchall()
print("Unsorted", results)

results.sort(key=sort_by_date_str)
print("Oldest->Newest", results)

results.sort(key=sort_by_date_str, reverse=True)
print("Newest->Oldest", results)
Output:
Unsorted [('6-5-1992', 6, 5, 1992), ('6-7-1962', 6, 7, 1962), ('6-11-1992', 6, 11, 1992)] Oldest->Newest [('6-7-1962', 6, 7, 1962), ('6-5-1992', 6, 5, 1992), ('6-11-1992', 6, 11, 1992)] Newest->Oldest [('6-11-1992', 6, 11, 1992), ('6-5-1992', 6, 5, 1992), ('6-7-1962', 6, 7, 1962)]
Reply
#6
Thanks for the solution above. I am now studying up on sorting so I can understand how you knew to do that.

In your key method i think you are taking the first field in the returned row, declaring it is m,d,y format separated by '-', and you are returning it to the sort in y,m,d format (which sorts as expected).
Something I can not figure out is you are not passing your method anything and it requires "x".

Just out of curiosity, since I have used .strptime and understand it a little better, and if a datetime sorts correctly, could the following be substituted into your method as well?
def sort_by_date_str(x):
    return datetime.strptime(x[0],'%m-%d-%y')
   
Reply
#7
It is assumed that the key function will be passed an item from the collection to be sorted. You can read about it here:

https://docs.python.org/3/howto/sorting.html

Datetime objects would work great for sorting, so a sorting key that created datetime objects from date strings would work great.
Reply
#8
Thank you.
I tried both and either give same result.
Problem resolved.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 349 Apr-17-2024, 10:17 AM
Last Post: CAD79
  Color a table cell based on specific text Creepy 11 1,959 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  reading a table which is of type string saisankalpj 2 957 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  Use module docx to get text from a file with a table Pedroski55 8 6,083 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,310 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Newbie Start with Text or Table? abrogard 5 2,516 Oct-18-2020, 12:47 PM
Last Post: jefsummers
  Count & Sort occurrences of text in a file oradba4u 7 3,066 Sep-06-2020, 03:23 PM
Last Post: oradba4u
  Taking serial data to perform key.press functions ausbollinger13 1 2,304 Sep-04-2020, 10:26 PM
Last Post: bowlofred
  Read/Sort Large text file avoiding line-by-line read using mmep or hdf5 Robotguy 0 2,044 Jul-22-2020, 08:11 PM
Last Post: Robotguy
  Type hinting - return type based on parameter micseydel 2 2,468 Jan-14-2020, 01:20 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020