comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Re: multidimensional sort
Date: Tue, 17 Mar 2020 17:24:01 +0000
Date: 2020-03-17T17:24:01+00:00	[thread overview]
Message-ID: <lyh7ymnbda.fsf@pushface.org> (raw)
In-Reply-To: 22e890a6-0be9-4c84-8108-5fdc24c57915@googlegroups.com

Petter Fryklund <petter.fryklund@atero.se> 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.

  reply	other threads:[~2020-03-17 17:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-17 12:31 multidimensional sort Gilbert Gosseyn
2020-03-17 12:56 ` Petter Fryklund
2020-03-17 17:24   ` Simon Wright [this message]
2020-03-17 20:16     ` Simon Wright
2020-03-17 16:08 ` Jeffrey R. Carter
2020-03-17 17:10 ` Optikos
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox