From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED.DFxaDADwSkn+6wW9Ah21PA.user.gioia.aioe.org!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: multidimensional sort Date: Tue, 17 Mar 2020 17:24:01 +0000 Organization: Aioe.org NNTP Server Message-ID: References: <2e1f10ed-a442-4c69-a97c-5f184940a245@googlegroups.com> <22e890a6-0be9-4c84-8108-5fdc24c57915@googlegroups.com> NNTP-Posting-Host: DFxaDADwSkn+6wW9Ah21PA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain X-Complaints-To: abuse@aioe.org User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (darwin) X-Notice: Filtered by postfilter v. 0.9.2 Cancel-Lock: sha1:a1G0OA+K4IO7HAAJyjjANKjk+Ck= Xref: reader01.eternal-september.org comp.lang.ada:58212 Date: 2020-03-17T17:24:01+00:00 List-Id: Petter Fryklund writes: > Den tisdag 17 mars 2020 kl. 13:31:43 UTC+1 skrev Gilbert Gosseyn: >> I want to sort an n-dim array by ascending dimensions and ascending >> elements as keys, in Ada. For example: >> consider an array with n = 5 like: >> >> ((5, 8, 21, 37, 46), (5, 7, 12, 19, 26), >> (10, 12, 19, 33, 44), (7, 8, 34, 36, 38), (5, 11, 12, 27, 32), (10, >> 16, 30, 41, 45), (10, 13, 19, 40, 45), (10, 11, 18, 32, 41), (11, >> 28, 34, 37, 38)) >> >> which shall become >> >> ((5, 7, 12, 19, 26), (5, 8, 21, 37, 46), (5, 11, 12, 27, 32), (7, 8, >> 34, 36, 38), (10, 11, 18, 32, 41), (10, 12, 19, 33, 44), (10, 13, >> 19, 40, 45), (10, 16, 30, 41, 45), (11, 28, 34, 37, 38)) >> >> How to store, how to sort, and how to deallocate the original? >> >> Thank you for your advice. > > This looks very much like homework. Have a look at > Ada.Containers.Indefinate_Vectors and the generic procedure > Generic_Sorting. Unless OP has some unstated requirements, I'd think that just Ada.Containers.Generic_Array_Sort would do the trick: type Arr is array (1 .. 5) of Positive; type Arrays is array (Positive range <>) of Arr; function "<" (L, R : Arr) return Boolean; procedure Sort is new Ada.Containers.Generic_Array_Sort (Index_Type => Positive, Element_Type => Arr, Array_Type => Arrays); The trick is to get "<" right! OP, what's wrong with Data : Arrays := ((5, 8, 21, 37, 46), (5, 7, 12, 19, 26), ... (11, 28, 34, 37, 38)); which doesn't need deallocation at all.