Bingo!
Do you think this should work on large lists... say a 100 - 200 or so ?
I will set out to test.
Tony Tanzillo wrote:
> Try this:
>
> (setq fuzz 1.0e-4) ;; numeric precision
>
> ;; Recursive comparison function:
>
> (defun compare-sub-elements (a b sortspec)
> ( (lambda (x y test)
> (if (and (equal x y fuzz) (cdr sortspec))
> (compare-sub-elements a b (cdr sortspec))
> (apply test (list x y))
> )
> )
> (nth (cdar sortspec) a)
> (nth (cdar sortspec) b)
> (caar sortspec)
> )
> )
>
> ;; Main function used in place of vl-sort:
>
> (defun complex-sort (alist sortspec)
> (vl-sort alist
> '(lambda (a b)
> (compare-elements a b sortspec)
> )
> )
> )
>
> ;; TEST
>
> (defun C:TESTIT ( / lst)
> (setq lst
> '( ("D" "a" 3 2 1)
> ("B" "c" 2 1 3)
> ("B" "a" 2 1 3)
> ("D" "b" 3 2 1)
> ("E" "a" 2 2 1)
> ("C" "b" 1 3 2)
> ("D" "b" 0 2 1)
> )
> )
> (mapcar 'print
> (complex-sort
> lst
> '((< . 1) (< . 3) (< . 0)) ; sort specification
> )
> )
> (princ)
> )
>
> The "sort specification" has the following form:
>
> ((
. ) ...)
>
> Where is a function that compares
> two elements (such as > or <), and is
> is the 0-based index of the elements of each
> sublist to compare using .
>
> There is one dotted pair of .
> for each sub-element that you want to sort the
> list on.
>
> "Mike Hutchinson" wrote in message
> news:3D5131D9.5491AE63@bbsae.com...
> > OK... what I am looking for is that you could pass in lists of vertually
> any
> > size (all the same number of elements of course)
> > but mixed types... string integers or reals
> > right now the application I have in mind uses 10 elements.
> > Then specify in a second parameter list specifiing just how you want it
> sorted
> >
> > a little complex but you get the idea ?
> > (setq lst '(("D" "a" 3 2 1)("B" "c" 2 1 3)("B" "a" 2 1 3)("D" "b" 3 2
> 1)("E"
> > "a" 2 2 1)("C" "b" 1 3 2)("D" "b" 0 2 1)))
> > (setq sortorder (list (cons 2 '<)(cons 1 '<)(cons 3 '<)...)
> >
> > (Sorttheselists LstOfLists SortOrderSpec)
> >
> > what I would want this to do is sort the list
> > first by the 2nd element in a ascending order,
> > then by the 4th element in a ascending order,
> > lastly by the 3rd element in a ascending order.
> >
> > in any one call the number of sort specs could not exceed the number of
> > elements in each list.
> > all the lists would have to be the same number of elements.
> > all lists would have to be in the same order with regards to data type...
> (str
> > int int real str str)
> >
> > with the above sort specs the result should be like this...
> > (("D" "b" 0 2 1) ("C" "b" 1 3 2) ("B" "a" 2 1 3) ("E" "a" 2 2 1) ("B" "c"
> 2 1
> > 3) ("D" "a" 3 2 1) ("D" "b" 3 2 1))
> >
> > this is quit similar to how excel sort is... (I know I could harness excel
> to
> > do it for me perhaps but I don't have the knowledge to get lisp (or VBA)
> to do
> > it... )
> >
> > The only other problem this might have in my application is the number of
> > lists could be quit large perhaps as many as 100 or more, but probably not
> as
> > many as 500. In my experience older programs using older bubble sort
> > functions could error out exceeding the stack (forget what the stack was
> > called just now)
> >
> > anyway... that's it.
> >
> > Mike Hutchinson wrote:
> >
> > > OK... let me compile a more complex list and attach it... I'll be back!
> > >
> > > Mike Hutchinson wrote:
> > >
> > > > If I want to sort a list of lists... say on the 3rd element of each
> how
> > > > might I go about this?
> > > >
> > > > The following logic is what I thought of ?
> > > >
> > > > process the lists moving all the 3rd elements to the head of each
> list.
> > > > extract all the car elements (was the 3rd el) into their own list.
> > > > sort this with acad_strlsort if each is a string or with a number
> > > > sorting routine if they are integers or reals.
> > > > reprocess the original list with a foreach on the sorted car elements
> > > > moving them from the origninal lists to a new list.
> > > > process the new sorted list moving all the car elements back to the
> 3rd
> > > > position.
> > > >
> > > > problem...
> > > > If I have two or more lists that are similar like
> > > >
> > > > (4 "MIKE" "MARY" "BECKY" "ALICE")
> > > > (4 "MIKE" "BILL" "BECKY" "ALICE")
> > > > (4 "MIKE" "ANNA" "BECKY" "ALICE")
> > > >
> > > > How can I get them to sort like this:
> > > > (3 "MIKE" "ANNA" "BECKY" "ALICE")
> > > > (3 "MIKE" "BILL" "BECKY" "ALICE")
> > > > (3 "MIKE" "MARY" "BECKY" "ALICE")
> > > >
> > > > In this case all three lists have an identical 3rd element, but in
> each
> > > > list the 2nd elements are different.
> > > > I suppose I would want them to sort first on the 3rd element then on
> the
> > > > 1rst element.
> > > > but how would my application know just how the lists vary or are
> similar
> > > > but not exactly the same.
> > > >
> > > > Any thougts?
> >