Recent

Author Topic: [Solved] Sorting column in a 2D array  (Read 6718 times)

Amoenet

  • New member
  • *
  • Posts: 8
[Solved] Sorting column in a 2D array
« on: June 10, 2014, 06:29:35 pm »
Hello,

I'm looking for a relative simple code to sort elements in a 2D array.
For instance, assume following array:
123   2   0    2   8
82     1   1    2   5
35     3   0    1   10
78     0   3    1   1

First column would be a team ID number, second column games won, losses, draws and finally team points in the last column.
Now I would want to sort this array so it shows the team with the highest points first, which means the entire row needs to switch place.

35     3   0    1   10
123   2   0    2   8
82     1   1    2   5
78     0   3    1   1



I can apply most sorting algoritmes in a single dim. array, not sure how to go about this in a 2D array. Bubblesort seems to be the simplest solution.
In case of two teams having an equal amount of points, the team with the most wins takes position over the other.

Would love if someone could take a look at it and write down a simple solution. I would adapt the code to use in a procedure so I can use it multiple times. (After each series of matches)
« Last Edit: June 10, 2014, 11:07:46 pm by Amoenet »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Sorting column in a 2D array
« Reply #1 on: June 10, 2014, 06:47:15 pm »
Quote
I can apply most sorting algoritmes in a single dim. array, not sure how to go about this in a 2D array. Bubblesort seems to be the simplest solution.

It is the same here. You will compare two numbers (from the last column). Instead of swapping those numbers, you will swap whole rows.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Amoenet

  • New member
  • *
  • Posts: 8
Re: Sorting column in a 2D array
« Reply #2 on: June 10, 2014, 10:22:06 pm »
To swap the row.... do I swap the all the values of that row individually?
Or can I do it in a for loop?


for i:=0 to 4 (in this case) do
begin
   temp:=ranking[2, i]
   ranking[2, i] := ranking[0, i]
   ranking[0, i] := temp
end;


If I'm not mistaken, this code would swap the entire 3th row with the first row, right?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Sorting column in a 2D array
« Reply #3 on: June 10, 2014, 10:35:53 pm »
Well, the convention is to index [x,y] where the first is usually considered as a horizontal and the second vertical. From this point of view, your code will swap columns 0 and 2.
But, of course, you don't have to follow this convention.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Amoenet

  • New member
  • *
  • Posts: 8
Re: Sorting column in a 2D array
« Reply #4 on: June 10, 2014, 10:45:33 pm »
Does it?

Convention then goes as follows?

[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2] [1,3]
[2,0] [2,1] [2,2] [2,3]

My swap takes every element of row 2 [2,i] and swaps it with the first row [0,i], doesn't it?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Sorting column in a 2D array
« Reply #5 on: June 10, 2014, 10:57:21 pm »
Yes, sorry, your code is fine. I'm thinking too much.

Quote
Convention then goes as follows?

[0,0] [0,1] [0,2] [0,3]
[1,0] [1,1] [1,2] [1,3]
[2,0] [2,1] [2,2] [2,3]

No.

[0, 0] [1, 0] [2, 0] [3, 0]
[0, 1] [1, 1] [2, 1] [3, 1]
[0, 2] [1, 2] [2, 2] [3, 2]
« Last Edit: June 10, 2014, 11:03:20 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Amoenet

  • New member
  • *
  • Posts: 8
Re: Sorting column in a 2D array
« Reply #6 on: June 10, 2014, 11:07:33 pm »
No worries, thanks for the reply!

Just hope the implementation into my program will go smoothly. Will be a nice challenge to write something like this in my program, for a very novice programmer that is.  8-)

 

TinyPortal © 2005-2018