Sorting tables by string key (which is a date) ?

I’ve been trying to do something simple but it’s been taking me ages since I can’t think of a way to solve this.

I need the code below to show the user’s screenshots on a panel, which it does perfectly fine.

The problem is, I need them to be IN ORDER of the dates they were taken (which I have a string for), but I’m not sure how I should be ordering them, since pairs doesn’t work and ipairs seemingly does nothing.

The screenshots should also be in groups depending on what GetNameForTime returns for each of them. For example, if a photo was taken this year on the 6th of January, any photos taken on that day (no matter the time) would be put into under that group.

This is my code:

concommand.Add( 'ShowMyScreenshots', function()
	local pics = file.Find( "screenshots/*", "MOD" )
	local matpath = '../screenshots/'
	local newpics = {}
	local function GetNameForTime( time )
		time = tonumber(time)
		if '%d %B %Y', time ) == '%d %B %Y' ) then
			time = 'Today'
			elseif '%Y', time ) == '%Y' ) then
			time = '%d %B', time )
			time = '%d %B %Y', time )
		return time
	for k, img in pairs( pics ) do
		local time = file.Time( 'screenshots/' .. img, 'MOD' )

		if string.GetExtensionFromFilename( img ) == 'png' or string.GetExtensionFromFilename( img ) == 'jpg' then
			newpics[ GetNameForTime( time ) ] = newpics[ GetNameForTime( time ) ] or {}
			newpics[ GetNameForTime( time ) ][ #newpics[ GetNameForTime( time ) ] + 1 ] = img
	local panel = vgui.Create( 'DFrame' )
	panel:SetTitle( 'Your screenshots' )
	panel:SetSize( 640, 400 )
	panel.Paint = function(self,w,h)
		surface.SetDrawColor( 0, 0, 0, 200 )
		surface.DrawRect( 0, 0, w, h )
	local scroll = vgui.Create( 'DScrollPanel', panel )
	scroll:Dock( FILL )
	for time, pics in pairs( newpics ) do
		local text = vgui.Create( 'DLabel', scroll )
		text:Dock( TOP )
		text:SetText( time )
		local imgpanel = vgui.Create( 'DIconLayout', scroll )
		imgpanel:Dock( TOP )
		imgpanel:SetSpaceX( 5 )
		imgpanel:SetSpaceY( 5 )
		for _, imgpath in pairs( pics ) do
			local t = imgpanel:Add( 'DImageButton' )
			t:SetSize( 150, 90 )
			t:SetImage( matpath .. imgpath )
			imgpanel:Add( t )
end )

I’m just not sure how to approach this, or where to put the original time variable, or even to use it at all.

maybe this?

then input the data into the panel

Or this:

I’ll try SortedPairs, but then I still have the issue of SortedPairs not sorting the non-modified time strings.

The thing is, if I change the time- say it’s something like 600000 (although that wouldn’t actually convert) and it converted to the 6th of December 2015 as a string.

Then, if I use the 6th of December as the table’s key, since months aren’t alphabetical it wouldn’t be in order.

But, if I try to use 600000 as the key instead, then how am I going to be able to convert 600000 to the 6th of December (as well as every other key in the table) and then get the 6th of December to be in the right place (when I’d have to merge categories that had the same date, which is pretty hard since they’re just DLabels).

If you don’t know what the hell I’m talking about, try any sort of method at all to sort the keys of the table while maintaining the converted string.

I think you need to think about the structure in general because if you use a date string that isnt exactly nice to sort why not have a table like

times[timestamp] = { DateString = “whatever” }

Lua also offers date/time functions which can convert a date to a timestamp and vise versa.