WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   sort by date (https://www.wowinterface.com/forums/showthread.php?t=58057)

nirran 06-17-20 08:33 PM

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


Ketho 06-17-20 08:58 PM

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

nirran 06-17-20 09:14 PM

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

Ketho 06-17-20 10:03 PM

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


nirran 06-17-20 10:43 PM

thank you very much,my limited scripting skills ,the code is erroring and i donno what the cause is

Kanegasi 06-18-20 02:22 AM

No one will be able to figure out the cause either if you don't share the error.

nirran 06-18-20 09:12 AM

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

SDPhantom 06-19-20 02:42 AM

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.


All times are GMT -6. The time now is 09:28 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI