Sort Array

Started by knowbots, December 23, 2008, 08:15:27 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

knowbots

Hello people

I have a Array  a(n) = array(j,k,l,m,n,o)

i need to sort by j,m,k  can somebody help-me

i lose 1 day to do this and nothing !!!

Thanks all

joemako

If you are just looking to swap elements in an array, http://www.freevbcode.com/ShowCode.Asp?ID=2537 may work for you.

If you are looking to do some other things, http://www.cpearson.com/excel/VBAArrays.htm may have some functions you are looking for. Be aware, most of these functions are looking for 2-Dimensional arrays, so they will need to be modified for 1-Dimensional arrays.

knowbots

ok man

But i need to 3 dimensions, i'm really i don't no what i do ....

Paul Herber

I must admit that I thought your question was about sorting an array containing 5 elements.
Use a search engine to look for 3D array sort, then extend it to 5D if you can. It depends on the relationship between the various dimensions, then choose your sort method. Why do you have a 5D array? How many elements?
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

wapperdude

If you are looking to do 3 independent sorts, I think you will need 3 separate arrays, each with it's own sort variable.  Before Excel became dominant, there was a program called ProCalc 3D that could do this, the 3rd dimension was separate worksheets.  Boy, that would fill things in a hurry.  Excel does sort of a nested sort, e.g., 1st by columnA, then by columnB, then by columnC.  That's probably not what you're after. 

If you are looking to plot data, there ought to be software that does plotting based upon 3-axis.

Hope this stimulates ideas.   :)
Wapperdude
Visio 2019 Pro

joemako

#5
Quote from: knowbots on December 24, 2008, 11:45:24 AM
But i need to 3 dimensions, i'm really i don't no what i do ....

What do you mean by 3 dimensions? do you mean 3 elements, or a nesting of 3 arrays?

In your initial message you gave an example of a 1-dimensional array with 6 elements: "array(j,k,l,m,n,o)"

That leads me to ask what do you mean by "sort by j,m,k"?

Can you give an example of what you want to put into a function and what you want the function to return?

for example:
Input: array(j,k,l,m,n,o)
Output: array(j,m,k)

vojo

I am more an engineer than a programmer...so there may be better ways to do this.
That said, what I would be something like the following:

For each j entry in the array
   Create a index that reflects the j value     //need to determine if first, last, or some middle value of j
   Create a index that reflects the k value     //same with k
   Create a index that reflects the m value    //same with m
   Use these index to create a super index    // oner example, jindx <<16 + kindx << 8 + mindx
   Place super index in a 1 dimensional array  where superarray
  • is in the same location as array element
       // superarray[0] = superindex for the 0th entry of the original array
       // superarray[1] = superindex for the 1th entry of the original array.
       // etc 
    Next

    //With this in place, I think the problem turns into a simple bubble sort
    //bubble sort the superarray and the original array as done below (if my memory serves me)


    Finished = false
    Do while finished = false
      finished = true
      for each x entry in superarray
         if entry x+1 < x then
            swap x and x+1
            swap entries in original array
            finished = false
         end if
       next
    until

    //now the superarray and the original array are sorted based on J,k,m  (J being most important)