Re: Sorting Multidimensional Arrays



IanW wrote:
>
> How do you sort a multidimensional array by a particular column?
>
> eg: how would you sort the following array by people's age?
>
> MyArray(0,0) = Bob
> MyArray(0,1) = 52
> MyArray(1,0) = Steve
> MyArray(1,1) = 38
> MyArray(2,0) = Anne
> MyArray(2,1) = 39

In any sort routine (and there are dozens of examples on the net) there is a point at which you compare two items, and another point at which you swap two items.

In this case, you would compare the (,1) elements, but swap both the (,0) and (,1) elements if called for.

Often it's better to use a separate one-dim index array with identical bounds as the (0,) dimension of the two-dim array. Compare and swap 'through' this array, so that your data array remains unchanged but can be accessed in sorted order.

But I would never use the arrangement you show to begin with. I would either construct a user-defined type that has the appropriate data type for each element, and create a one-dim array of that. Or a class that contains the data, and has an Index array property and a Sort method that will arrange the Index in the order you like.

--

Jim Mack
MicroDexterity Inc
www.microdexterity.com
.



Relevant Pages

  • Re: Need help sorting by specific fields in file.
    ... > I have a file that I need to sort and currently I am just ... You're sorting an array. ... grab the first 6 characters off of all of them, ... That's just a basic grab the first 6 numbers and compare approach. ...
    (perl.beginners)
  • Re: An inefficiency in Array.Sort
    ... use a temporary array, and Array.Sort is an in-place sort. ... they repeatedly compare ato a. ... former really makes no sense at all, especially presuming a QuickSort ... It is possible that the sort algorithm loses track of the original array ...
    (microsoft.public.dotnet.languages.vb)
  • Re: "Sorting" assignment
    ... And many others prefer to call partition exchange because "quicksort" ... bin B depending on whether it is greater than, ... If the array is already sorted, this means that you end up ... attempt to sort them. ...
    (comp.programming)
  • Re: Fortran templates
    ... into "having a method which sorts the client-side array". ... have to explicitly define the type of data to compare. ... code, whereas in my Fortran quicksort-with-callbacks, you must define ... However, if you want to sort *strings*, you will have to define ...
    (comp.lang.fortran)
  • Re: A Fast sorting algorithm for almost sorted data
    ... far my compressor has potential but is nowhere near ready. ... It does however make heavy use of sorting. ... which I am currently calling Run sort. ... entire selected run can be added to the sorted output array. ...
    (comp.compression)

Loading