[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: natural order comparison?
- From: PA <petite.abeille@...>
- Date: Thu, 22 Dec 2005 00:43:08 +0100
Hello,
I'm trying to implement a "natural order" comparator as described here:
http://www.naturalordersort.org/
http://sourcefrog.net/projects/natsort/
Something along these lines:
local aList = { "rfc1000.txt", "rfc2086.txt", "rfc822.txt" }
table.sort( aList, compare )
table.foreach( aList, print )
> 1 rfc822.txt
> 2 rfc1000.txt
> 3 rfc2086.txt
Here is the implementation I have so far:
local function list( aValue )
local aValue = tostring( aValue ):lower()
local aList = {}
for aNumber, aString in aValue:gmatch( "(%d*%.?%d*)(%D*)" ) do
aList[ #aList + 1 ] = tonumber( aNumber )
aList[ #aList + 1 ] = aString
end
return aList
end
local function compare( aValue, anotherValue )
local aList = list( aValue )
local anotherList = list( anotherValue )
for anIndex, aValue in ipairs( aList ) do
local anotherValue = anotherList[ anIndex ]
if aValue ~= anotherValue then
if type( aValue ) == type( anotherValue ) then
return aValue < anotherValue
else
return tostring( aValue ) < tostring(
anotherValue )
end
end
end
return false
end
Seems to.. hmmm... work... but... I was wondering if someone has
perhaps some ideas for a more straightforward implementation... In
particular, is there a way the construct the regular expression
"(%d*%.?%d*)(%D*)" so it returns only one capture, either one made only
of digits or characters?
TIA!
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/