lexorder - Maple Help

# Online Help

###### All Products    Maple    MapleSim

lexorder

test for lexicographical order

 Calling Sequence lexorder(s1, s2)

Parameters

 s1, s2 - strings or unevaluated symbols

Description

 • The procedure lexorder returns true if s1 occurs before s2 in lexicographical order, or if s1 is equal to s2.  Otherwise, it returns false.
 • The lexicographical order depends in part on the ordering of the underlying character set, which is system-dependent.
 • For strings and symbols consisting of ordinary letters, lexicographical order is the standard alphabetical order.
 • The most common use of lexorder is as the second (optional) argument to the sort command. This allows you to sort a list of strings (or symbols) lexicographically. Note, however, that it is the symbol lexorder rather than the lexorder procedure that should be used as an option to sort. Both will work, with identical results, but the symbol lexorder is recognized by sort, causing it to use a builtin algorithm that is faster than the one that calls the lexorder procedure. (See the example below.)

Thread Safety

 • The lexorder command is thread-safe as of Maple 15.
 • For more information on thread safety, see index/threadsafe.

Examples

 > $\mathrm{lexorder}\left(a,b\right)$
 ${\mathrm{true}}$ (1)
 > $\mathrm{lexorder}\left(A,a\right)$
 ${\mathrm{true}}$ (2)
 > $\mathrm{lexorder}\left("a",a\right)$
 ${\mathrm{true}}$ (3)
 > $\mathrm{lexorder}\left(\mathrm{greatest},\mathrm{great}\right)$
 ${\mathrm{false}}$ (4)
 > $\mathrm{lexorder}\left(\mathrm{*},\mathrm{^}\right)$
 ${\mathrm{true}}$ (5)
 > $\mathrm{lexorder}\left("first","second"\right)$
 ${\mathrm{true}}$ (6)
 > $\mathrm{sort}\left(\left["first","second","third","fourth","fifth"\right],'\mathrm{lexorder}'\right)$
 $\left[{"fifth"}{,}{"first"}{,}{"fourth"}{,}{"second"}{,}{"third"}\right]$ (7)

Calling the builtin procedure lexorder() directly is slower than using the 'lexorder' algorithm that is built in to sort().

 > $L≔\left[\mathrm{seq}\left(\mathrm{StringTools}:-\mathrm{Random}\left(1000,'\mathrm{alnum}'\right),i=1..10000\right)\right]:$
 > $\mathrm{time}\left(\mathrm{sort}\left(L,'\mathrm{lexorder}'\right)\right)$
 ${0.007}$ (8)
 > $\mathrm{time}\left(\mathrm{sort}\left(L,\mathrm{eval}\left(\mathrm{lexorder}\right)\right)\right)$
 ${0.198}$ (9)

Reversing the sense of the comparison is more costly still, because a full Maple procedure call is incurred for each comparison.

 > $\mathrm{time}\left(\mathrm{sort}\left(L,\left(a,b\right)↦\mathrm{lexorder}\left(b,a\right)\right)\right)$
 ${0.339}$ (10)

A better way to do this is to use a linear reversal algorithm after sorting the list with the builtin algorithm. Since sorting dominates at O(n*ln(n)), using the sorting algorithm with the smaller constant factor delivers better performance.

 > revsort := proc( los::list(string) )::list(string);     local    L;     L := sort( los, 'lexorder' );     [ seq( L[ -i ], i = 1 .. nops( L ) ) ] end proc:
 > $\mathrm{time}\left(\mathrm{revsort}\left(L\right)\right)$
 ${0.013}$ (11)
 > $\mathrm{evalb}\left(\mathrm{revsort}\left(L\right)=\mathrm{sort}\left(L,\left(a,b\right)↦\mathrm{lexorder}\left(b,a\right)\right)\right)$
 ${\mathrm{true}}$ (12)

 See Also