Quantcast
sort by date - WoWInterface
Thread Tools Display Modes
06-17-20, 08:33 PM   #1
nirran
A Murloc Raider
Join Date: Mar 2005
Posts: 8
sort by date

having a hell of a time with function

if i only sort by day it works,but when i add month and year it screws everything up

this is what i have


Code:
function dateSort(dateTable)
    local i = 1
    local changed = false

    table.foreach(
        dateTable,
        function(k, v)
            local day = string.sub(dateTable[k], 0, 2)
            local month = string.sub(dateTable[k], 3, 4)
            local year = string.sub(dateTable[k], 5, 6)
			--print("Date " .. dateTable[k])

            if (i < table.getn(dateTable)) then
                local nextDay = string.sub(dateTable[k + 1], 0, 2)
                local nextMonth = string.sub(dateTable[k + 1], 3, 4)
                local nextYear = string.sub(dateTable[k + 1], 5, 6)
				--print("day " .. nextDay .. "month " .. nextMonth .. "year " .. nextYear)
                --if (tonumber(day) < tonumber(nextDay)) and (tonumber(month) <= tonumber(nextMonth)) --[[ and (tonumber(year) <= tonumber(nextYear))]] then
				--if (tonumber(day) < tonumber(nextDay)) and (tonumber(month) <= tonumber(nextMonth) or tonumber(month) < tonumber(nextMonth)) and (tonumber(year) <= tonumber(nextYear) or tonumber(year) < tonumber(nextYear)) then                
				--if (tonumber(day) < tonumber(nextDay)) and (tonumber(month) <= tonumber(nextMonth)) then	
				if (day < nextDay) and (month <= nextMonth) and (year <= nextYear) then
					changed = true

                    local temp = dateTable[k]
                    dateTable[k] = dateTable[i+1]
                    dateTable[i+1] = temp
                end
            end

            i = i + 1
        end
    )

    if (changed) then
        dateSort(dateTable)
    end
end
  Reply With Quote
06-17-20, 08:58 PM   #2
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 941
To make things easier for everyone, what does dateTable look like. Do you have an example

Crosspost for reference which also offers a better solution since we're not sure what you are actually trying to do
https://us.forums.blizzard.com/en/wo...by-date/558388

Last edited by Ketho : 06-17-20 at 09:03 PM.
  Reply With Quote
06-17-20, 09:14 PM   #3
nirran
A Murloc Raider
Join Date: Mar 2005
Posts: 8
this the table,new extry is addd when log in and none exists

Code:
SAVES = {
	["150620"] = 51745398698,
	["140620"] = 51547835967,
	["130620"] = 51534872357,
	["110116"] = 51534872357,
	["130619"] = 51534872357,
	["250618"] = 51534872357,
	["160620"] = 51989680638,
	["170620"] = 52179106978,
	["150618"] = 51534872357,
	["150517"] = 51534872357,
	["250518"] = 51534872357,
	["110517"] = 51534872357,
}
most of those i hand edited to give a table to work with

edit : should clarify the [] is date ddmmyy,and int is copper player has

Last edited by nirran : 06-17-20 at 09:51 PM.
  Reply With Quote
06-17-20, 10:03 PM   #4
Ketho
A Molten Giant
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 941
I think you should do what Gello said and store it in a more manageable format. There is probably a better approach to all this

As for sorting your example, this sorts DDMMYY
Lua Code:
  1. SAVES = {
  2.     ["150620"] = 51745398698,
  3.     ["140620"] = 51547835967,
  4.     ["130620"] = 51534872357,
  5.     ["110116"] = 51534872357,
  6.     ["130619"] = 51534872357,
  7.     ["250618"] = 51534872357,
  8.     ["160620"] = 51989680638,
  9.     ["170620"] = 52179106978,
  10.     ["150618"] = 51534872357,
  11.     ["150517"] = 51534872357,
  12.     ["250518"] = 51534872357,
  13.     ["110517"] = 51534872357,
  14. }
  15.  
  16. local t = {}
  17. for timestamp in pairs(SAVES) do
  18.     table.insert(t, timestamp)
  19. end
  20. table.sort(t, function(a, b)
  21.     local day1, month1, year1 = a:match("(%d%d)(%d%d)(%d%d)")
  22.     local day2, month2, year2 = b:match("(%d%d)(%d%d)(%d%d)")
  23.     if year1 ~= year2 then
  24.         return year1 < year2
  25.     elseif month1 ~= month2 then
  26.         return month1 < month2
  27.     elseif day1 ~= day2 then
  28.         return day1 < day2
  29.     end
  30. end)
  31.  
  32. for _, timestamp in pairs(t) do
  33.     print(timestamp, SAVES[timestamp])
  34. end
Code:
110116  51534872357
110517  51534872357
150517  51534872357
250518  51534872357
150618  51534872357
250618  51534872357
130619  51534872357
130620  51534872357
140620  51547835967
150620  51745398698
160620  51989680638
170620  52179106978
  Reply With Quote
06-17-20, 10:43 PM   #5
nirran
A Murloc Raider
Join Date: Mar 2005
Posts: 8
thank you very much,my limited scripting skills ,the code is erroring and i donno what the cause is
  Reply With Quote
06-18-20, 02:22 AM   #6
Kanegasi
A Firelord
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 499
No one will be able to figure out the cause either if you don't share the error.
  Reply With Quote
06-18-20, 09:12 AM   #7
nirran
A Murloc Raider
Join Date: Mar 2005
Posts: 8
awesome,got it working all cept the orderis backwards,this si what i have

Code:
function dateSort(dateTable)

    table.sort(
        dateTable,
        function(k, v)
			local day1, month1, year1 = k:match("(%d%d)(%d%d)(%d%d)")
			local day2, month2, year2 = v:match("(%d%d)(%d%d)(%d%d)")
			if year1 ~= year2 then
				return year1 < year2
			elseif month1 ~= month2 then
				return month1 < month2
			elseif day1 ~= day2 then
				return day1 < day2
			end
        end
    )
end
how do i reversethe order?

edit : swapping logic worked,nother bug but closer,thnx ppl

Last edited by nirran : 06-18-20 at 11:12 AM. Reason: adding info
  Reply With Quote
06-19-20, 02:42 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,960
If you store dates in YYYYMMDD format, you can use table.sort() without a custom function. This greatly improves performance since the entire comparison happens in C code instead of calling your custom Lua function each time it wants to compare two entries.
__________________
ESOUI AddOns | WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » sort by date

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off