[ create a new paste ] login | about

Link: http://codepad.org/QakDRqwX    [ raw code | output | fork ]

Lua, pasted on Feb 23:
function compareTime(t1,t2) 
	print("Comparing: "..unpack(t1).."-"..unpack(t2))
	--split t1
 	hms_t1 = split(t1,":")
	--explicit conversion due to lack of coercion for < operator
	h1=tonumber(hms_t1[1])
	m1=tonumber(hms_t1[2])
	s1=tonumber(hms_t1[3])
	--split t2
	hms_t2 = split(t2,":")
	--explicit conversion due to lack of coercion for < operator
	h2=tonumber(hms_t2[1])
	m2=tonumber(hms_t2[2])
	s2=tonumber(hms_t2[3])
	
	if (h1 < h2) or (h1==h2 and m1 <= m2) or (h1==h2 and m1==m2 and s1 <= s2) then
		return true
	else
		return false
	end
end

local t = {}
t["03:05:30"] = 80
t["04:05:21"] = 40
t["01:03:03"] = 40
print("Before sort")
for k,v in pairs(t) do
	print(k,v)
end
table.sort(t, compareTime)
print("After sort")
for k,v in pairs(t) do
	print(k,v)
end


Output:
1
2
3
4
5
6
7
8
Before sort
04:05:21	40
01:03:03	40
03:05:30	80
After sort
04:05:21	40
01:03:03	40
03:05:30	80


Create a new paste based on this one


Comments: