using tellsimp for sorting



Hello.

The sorting problem.

We have a product of terms s[i,j] eg:
s[3,2] . s[2,2] . s[2,1] . s[3,3]
I use dot product because I need reorder this terms using my own rules.
And the rules are such that terms should be sorted by first index but 
order of second index must be intact.
So above product after sorting shoul look like:
s[2,2] . s[2,1] . s[3,2] . s[3,3]

I wrote some code using examples from this forum:

greaterthan(i,j):=integerp(j) and is(i>j)$
matchdeclare([ii,jj],integerp)$
matchdeclare(i,integerp,j,greaterthan(i))$
tellsimp(s[i,ii].s[j,jj],s[j,jj].s[i,ii])$

It sorts only two rightmost term. Try this examples:
    s[4,2] . s[3,1];
    s[4,2] . s[3,1] . s[2,3];
    s[4,2] . s[3,1] . s[2,3] . s[1,1];

The output is:
    s[3,1] . s[4,2]
    s[4,2] . s[2,3] . s[3,1]
    s[4,2] . s[3,1] . s[1,1] . s[2,3]
but I'd like it to be:
    s[3,1] . s[4,2]
    s[2,3] . s[3,1] . s[4,2]
    s[1,1] . s[2,3] . s[3,1] . s[4,2]
    

Piotr Slomski
pslomski at tlen.pl