Path: utzoo!mnetor!tmsoft!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!ukc!tcdcs!dce.ie!em From: em@dce.ie (Eamonn McManus) Newsgroups: comp.text.tex Subject: Re: Two problems with my directory format Message-ID: Date: 12 Feb 91 22:04:58 GMT References: <59099@eerie.acsu.Buffalo.EDU> Organization: Datacode Communications Ltd, Dublin, Ireland Lines: 39 xiaofei@acsu.buffalo.edu (Xiaofei Wang) writes: >Another thing is one has to enter the directory entry >alphabetically manually. [I looked the phone.tex and my impression is >it does not have ``auto ordering'' feature.] Since TeX does not have primitives to compare two strings except for equality, it is difficult to use it to do ordering. Besides, doing it by hand means that you can deal with peculiarities of your national language, or put names with the forename first even though they are sorted by the surname. Of course, it's *possible* to write macros to compare strings (naively) and to sort entries (e.g., by building a binary tree as the entries are read); it's just not the sort of thing that's best done in TeX. Here are some macros I wrote some time ago to compare strings alphabetically. \compare{string1}{string2} expands to 0, -1, or 1, according as string1 is the same as, less than, or greater than string2, respectively. Both strings should consist entirely of plain characters (not control sequences). I took care that the comparison is done entirely in TeX's `mouth', so this macro can be used in \ifnum and the like. , Eamonn % compare.tex - macro to compare two strings % By \'Eamonn McManus. This file is not copyrighted. \def\compare#1#2{\compcont#1\ \\#2\ \\} \def\compcont#1#2\\#3#4\\{% \csname comp% \if #1#3\if #1\ same\else cont\fi \else \ifnum `#1<`#3less\else greater\fi \fi \endcsname #2\\#4\\} \def\compsame#1\\#2\\{0} \def\compless#1\\#2\\{-1} \def\compgreater#1\\#2\\{1}