473,900 Members | 4,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting 2D-array

132 New Member
Hello,

I'm trying to get my 2D-array sorted but for some reason I can't seem to get it sorted in the right order. Anybody that can help me find the solution?

I have an array which has been constructed as follows
Expand|Select|Wrap|Line Numbers
  1. array (Name, Number1, Number2, Total, Previous Position, New Position)
  2.  
I'm using the last two columns as a way to define the order.
The value that needs to be sorted is the 4th column (Total).
But instead of actually changing the indexes, I want to change the value of the column New Position.

So to give you an example:

BEFORE SORT
Expand|Select|Wrap|Line Numbers
  1. person(0) ("NAME1", 4, 51, 55,1,1)
  2. person(1) ("NAME2", 12, 41, 53,2,2)
  3. person(2) ("NAME3", 24, 35, 59,3,3)
  4. person(3) ("NAME4", 1, 11, 12,4,4)
  5. person(4) ("NAME5", 0, 22, 22,5,5)
  6.  
AFTER SORT
Expand|Select|Wrap|Line Numbers
  1. person(0) ("NAME1", 4, 51, 55,1,2)
  2. person(1) ("NAME2", 12, 41, 53,2,3)
  3. person(2) ("NAME3", 24, 35, 59,3,1)
  4. person(3) ("NAME4", 1, 11, 12,4,5)
  5. person(4) ("NAME5", 0, 22, 22,5,4)
  6.  
As you can see, only the value in the last column has changed depending on the value of the 4th column.

I'm using this last column to define the order in which I'm displaying this array. But I need to be able to keep track of the original position (the second to last column).

Right now I'm using the following code:

Expand|Select|Wrap|Line Numbers
  1. Dim Pos1 As Integer
  2. Dim Pos2 As Integer
  3.  
  4.     For i = 0 To (UBound(Person) - 1)
  5.  
  6.         'Previous Positions
  7.         Person(i, 4) = Person(i, 5)
  8.  
  9.         For j = i To UBound(Person)
  10.             If CInt(Person(i, 3)) < CInt(Person(j, 3)) Then
  11.                 Pos1 = CInt(Person(i, 5))
  12.                 Pos2 = CInt(Person(j, 5))
  13.  
  14.  
  15.                 ' New Positions
  16.                 Person(i, 5) = Pos2
  17.                 Person(j, 5) = Pos1
  18.  
  19.             End If
  20.         Next
  21.     Next
  22.  
Thanks for the help!
Nov 16 '12
12 3235
Rabbit
12,516 Recognized Expert Moderator MVP
I was under the impression that the starting positions matched the index. I got this impression from your sample data in posts #1, #3, #7. The algorithm I gave you only works using that assumption.

If, however, that is not the case, then you can easily make the data conform by setting the positions to the index before doing the sort.
Nov 23 '12 #11
Cainnech
132 New Member
Unfortunately, I can't change my index as I need to keep my Persons-array in the same order. The only way I can see that happening is if I add yet another dimension to the array which has the order in which it should appear.

In effect I don't think it will be possible to sort this array directly from itself. The only solution that I see now is to use a control that does the sorting for me.
Nov 23 '12 #12
Rabbit
12,516 Recognized Expert Moderator MVP
I didn't say you need to change the index. Just change the position to the index after setting the previous position and before the sort.
Nov 24 '12 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

14
11518
by: John Hunter | last post by:
I have a list of two tuples containing x and y coord (x0, y0) (x1, y1) ... (xn, yn) Given a new point x,y, I would like to find the point in the list closest to x,y. I have to do this a lot, in an inner loop, and then I add each new point x,y to the list. I know the range of x and y in
4
2541
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
0
571
by: beliavsky | last post by:
xiaohua_sun@yahoo.com (SunX) wrote in message news:<337e6cd5.0405291411.4376debc@posting.google.com>... > What is the best way to assign a 2d lists? Something like; > > for i in range(10): > for j in range(10): > aList = i*j > > Thank you in advance. (When I replied with subject "Re: 2d lists" Google groups complained.)
2
13922
by: Oldchatterman | last post by:
Hello, in an application I measure a lot of 2d coordinates (x,y) of a pattern. This pattern consists of a set of points on grid with fixed pitches in x and y direction. These coordinates all have a score for quality and are sorted on this score. What I want to do is to sort these coordinates first on x and define groups (regions) of x-coordinates that belong together. After this step I want to sort the different x-regions in y-regions.
0
1326
by: Simena Dinas | last post by:
KToon: 2D Animation Toolkit KToon is a 2D Animation Toolkit designed by animators (Toonka Films) for animators, focused to the Cartoon's industry. This project is covered by the GPL License using G++, OpenGL and QT as programming resources from KDevelop as the development platform. One year later of production, it is a pleasure for Toonka Films announces to the Free Software community the release of the KToon
2
3171
by: Shamli | last post by:
I am looking for an algorithm that enlarge a 2D polygon. cheers,
23
6363
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
1
7029
by: Jesper | last post by:
Hi, 1. I've search the net for some great usercontrols where I can plot data, draw graphs in both 2d and 3d. I've not had that much of success in my search, and I'm considering writing my own. Quite a cumbersome task compared to a freeware usercontrol. I've found one (2D), but it can't be customized in several way, font size, gridlines on/off, colours and so. Do any og you guys know of a great buyware/freeware usercontrol graph in c#...
5
4437
by: Jack Nielsen | last post by:
Does anyone have a link to Visual Studio 2005 directcx 2d sprite manipulation or just simple graphics with directx 2d. Jack -------------------------------------------------------------------------------- Jeg beskyttes af den gratis SPAMfighter til privatbrugere. Den har indtil videre sparet mig for at få 17573 spam-mails Betalende brugere får ikke denne besked i deres e-mails. Hent en gratis SPAMfighter her.
4
5243
by: Jon Harrop | last post by:
I am writing a 3D graphing component built upon WPF and would like to have 2D vector graphics (e.g. typeset mathematics) as labels laid out from 3D coordinates. For example, a tick on an axis has a 3D coordinate and its 2D label might be right-aligned to the 2D projection of that 3D point. The following Mathematica plot illustrates the functionality I am after: http://math.arizona.edu/~goriely/M322/Mathma-ComplexFunc.jpg I have done...
0
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11277
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6084
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4724
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3323
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.