From: David Hanley
Subject: Re: question on sort
Date: 
Message-ID: <39872A42.9A17A969@ncgr.org>
I am not sure what you are looking for.

What does sorting a higher dimensional array mean?

Why not sort a vector of vectors?

Answer these, and more help can be provided.

dave

From: Raymond Toy
Subject: Re: question on sort
Date: 
Message-ID: <4nlmyffydw.fsf@rtp.ericsson.se>
>>>>> "kp" == kp gores <·····@sip.medizin.uni-ulm.de> writes:

    kp> hm, donT know. probably because the definition would look like 
    kp> (make-array 20 :initial-element (make-array 6 )). whats wrong with 

This probably isn't what you want either.  Each element of the
20-element array is now pointing to exactly the same 6-element array:

* (setf z (make-array 3 :initial-element (make-array 6)))
#(#(0 0 0 0 0 0) #(0 0 0 0 0 0) #(0 0 0 0 0 0))
* (setf (aref (aref z 1) 3) "???")
"???"
* z
#(#(0 0 0 "???" 0 0) #(0 0 0 "???" 0 0) #(0 0 0 "???" 0 0))


Ray
From: David Hanley
Subject: Re: question on sort
Date: 
Message-ID: <39883B44.4DC255AB@ncgr.org>
kp gores wrote:

> In article <·················@ncgr.org>, David Hanley <···@ncgr.org>
> wrote:
>
> > I am not sure what you are looking for.
> > What does sorting a higher dimensional array mean?
>
> my array could be of dimensions (* 6) eg. for a files:
> there you could have entries
> name, owner, group ,size, creation date, modification date etc..
>
> sorting means to sort by name (key for sort column 0), or by creation
> date (key is column 4). even nicer it would be to sort by name as
> primary key and date as secondary key... you get the idea.

Ahh, I see.  I wasn't understanding earlier.

I think others are right; what is probably better is to do
something like the following:

(defstruct rec
  name
  blah
  blah
  creation-date
   ... )

(setq arr (make-array *n*))
(dotimes (i *n*)
  (setf (aref arr i) (make-rec)))




>
>
> >
> > Why not sort a vector of vectors?
>
> hm, donT know. probably because the definition would look like
> (make-array 20 :initial-element (make-array 6 )). whats wrong with
> (make-array '(20 6)) ??

Firstly, what's wrong is that every element of the array will point to
the same sub-array :) That's why I used a loop above.

What's wrong with the multidimensional array approach is that it's not
an array of things; rather it's 2d-array of all the same things.  That's
the reason why sorting it is hard.  Sorting a multidimensional array is
an ambiguous thing, because you could impose any arbitrary
structure on it.  For example, are columns records, or are rows?
Maybe two consecutive rows compose records?  What happens
when you get clever and use a 3 dimensional array?

But, for what you want, the following works.

(defun multisort( arr comp )
  "sort a multidimensional array--not in place, either"
  (let* (
         (size (length arr))
         (index-array (make-array size)))
    (dotimes(i size)
      (setf (aref index-array i) i))
    (sort index-array
#'(lambda(x y)(funcall comp (aref arr x) (aref arr y))))
    (map 'vector #'(lambda(x)(aref arr x)) index-array)))

;; testing code

(defparameter a #( #(1 2 3) #(2 2 2) #(3 4 1)))

(defun te( c )
  "test by sorting based on a specific column"
  (multisort a #'(lambda(x y)(< (aref x c)(aref  y c)))))

By the way, something like initial-contents that called a function
would be really keen.  That way you could fill the array
with individual (different) objects more easily.

dave