From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA27097 for caml-red; Sun, 3 Dec 2000 23:03:51 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA31919 for ; Thu, 30 Nov 2000 12:33:36 +0100 (MET) Received: from ext.lri.fr (ext.lri.fr [129.175.15.4]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id eAUBXZf09922 for ; Thu, 30 Nov 2000 12:33:35 +0100 (MET) Received: from pc87.lri.fr (IDENT:root@pc87 [129.175.8.106]) by ext.lri.fr (8.9.3/jtpda-5.3.2) with ESMTP id MAA11903 ; Thu, 30 Nov 2000 12:33:34 +0100 (MET) Received: from lri.fr (IDENT:jcourant@localhost.localdomain [127.0.0.1]) by pc87.lri.fr (8.9.3/jtpda-5.3.2) with ESMTP id MAA31268 ; Thu, 30 Nov 2000 12:33:34 +0100 Message-ID: <3A263B0E.379265F1@lri.fr> Date: Thu, 30 Nov 2000 12:33:34 +0100 From: Judicael Courant X-Mailer: Mozilla 4.72 [fr] (X11; U; Linux 2.2.14-5.0 i686) X-Accept-Language: en MIME-Version: 1.0 To: Mattias Waldau CC: caml-list@inria.fr Subject: Re: sorting of list of vectors (array with one dimension) References: Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Sender: weis@pauillac.inria.fr Mattias Waldau a écrit : > > I have a list of vectors, I would like to sort according to the first > vector, and move the corresponding data in the other vectors. > > Is there a function for this (for example, a sort function, where I add a > function that is called each time data is swapped) or do I have to write a > quicksort in ocaml. > > /mattias Maybe you are not using the right data representation ? Things would be much simpler if you used a vector of lists instead of a list of vectors ; just a matter of Array.sort (fun l1 l2 -> compare (List.hd l1) (List.hd l2)) lv Now, you can easily convert your initial representation to this one as follows (untested code): (* get : int -> 'a array list -> 'a list *) let get i vl = List.map (fun t -> t.(i)) vl (* lv_of_vl : 'a array list -> 'a list array *) let lv_of_vl vl = Array.init (Array.length (List.hd vl)) (fun i -> get i vl) (* set : int -> 'a list -> 'a array list -> unit *) let set i l vl = List.iter2 (fun x t -> t.(i) <- x) l vl (* vl_of_lv : 'a list array -> 'a array list -> unit *) let vl_of_lv lv vl = Array.iteri (fun i l -> set i l vl) lv Sincerly yours, Judicaël. -- Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/ (+33) (0)1 69 15 64 85 "Montre moi des morceaux de ton monde, et je te montrerai le mien" Tim, matricule #929, condamné à mort. http://rozenn.picard.free.fr/tim.html