Re: sorting dates
- From: Dr John Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 29 Jul 2005 22:35:26 +0100
JRS: In article <dccq76$22i$1@xxxxxxxxxxxxxxxxx>, dated Fri, 29 Jul
2005 03:43:07, seen in news:comp.lang.javascript, fox
<spamtrap@xxxxxxxxxxxxx> posted :
>
>
>comparing strings no doubt takes longer than comparing numbers.
Perhaps so. But the comparison operator should be implemented at the
ASM level or similar; so, for strings of the length in question, ISTM
reasonably likely that the overall time will be dominated by other parts
of the routine. If it matters, it should be tested.
With 8-character strings, using the quickest test loop I know, I find
the loop for comparison of equal 8-character Strings to be about 10%
slower than that for the corresponding Numbers, loop overhead being
about 20%. Other browsers are likely to differ.
>if all dates to be sorted are in YYYY-MM-DD format, and -- worst case,
>they are all in the first week of the same year and month, then the
>MINIMUM # of comparisons needed to be made to pass one as <> the other
>would be 10 (one per character as long as they are equal) - two week
>span, 9 -- etc... Unless, of course, JavaScript can compare "chunks" of
>strings in one "throw" (I used to do it in C -- up to 8 characters at a
>time -- typecast to longints -- really good way to speed up searches too)
IIRC, the PC CPU instruction set provides for efficient comparisons of
strings of 8-bit characters, and may do so, intentionally or otherwise,
foe 16-bit ones.
>oh... that little YYYYMMDD integer format -- it's called a "packed" date
>format (packing integers is a "time-honored programming tradition") --
>been around for considerably longer than JavaScript. Technically, the
>"digits" should be hexadecimal, but for sorting purposes, decimal digits
>work just as well with the extra added benefit of being easily readable
>in human terms.
It's not rightly called "packed". When discussing ISO 8601 formats, one
should use ISO 8601 terminology.
The string 'YYYYMMDD' is "Basic format".
The string 'YYYY-MM-DD' is "Extended format".
A Basic format string can (with unary +) be converted to a javascript
Number; but that is an IEEE Double, not an integer. Numbers in
javascript do not have decimal digits.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
.
- Follow-Ups:
- Re: sorting dates
- From: fox
- Re: sorting dates
- Prev by Date: Re: sorting dates
- Next by Date: Re: Date Expiry of Page using Javascript ?
- Previous by thread: Re: sorting dates
- Next by thread: Re: sorting dates
- Index(es):
Relevant Pages
|