Thread: sort by date
View Single Post
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