Visit Our UNIX and Linux User Community


Multidimensional arrays and sort.


 
Thread Tools Search this Thread
Top Forums Programming Multidimensional arrays and sort.
# 1  
Old 01-30-2011
Multidimensional arrays and sort.

My language is C++. I have a multidimensional vector that I would like to sort by a specific "cell" or "field" within the main vector. Does anyone have any information on how to do this? I have searched all over the internet and every reference manual I can find. So far I have found very little to no information on this topic.

Thank you
# 2  
Old 01-30-2011
seems difficult....I wrote a program to illustrate the situation of a two dimensions array,more dimensions will be more difficult to implement.
Code:
typedef struct {
	int a;
	int b;
}EMBED;

bool my_sort(EMBED a,EMBED b){
	return a.b > b.b;
}

int main()
{
	int a[10][2];
	EMBED embed_a[10];

	srand(time(NULL));	
	
	for(int i=0;i<10;i++)
		for(int j=0;j<2;j++)
			a[i][j] = rand()%200;
			
	for(int i=0;i<10;i++){		
		embed_a[i].a = a[i][0];
		embed_a[i].b = a[i][1];
	}
	
	vector<EMBED> vec(embed_a,embed_a+10);
	sort(vec.begin(),vec.end(),my_sort);

	for(int i=0;i<10;i++){
		for(int j=0;j<2;j++)
			cout<<a[i][j]<<" ";
		cout<<endl;
	}
	cout<<"============AFTER SORTING==============="<<endl;
	for(vector<EMBED>::const_iterator iter = vec.begin();
								iter != vec.end();iter++)
		cout<<iter->a<<" "<<iter->b<<endl;

	return 0;
}

# 3  
Old 01-30-2011
Binary sort of a table Fortran V.
Sort table on column 2 then 1.
The order of equal sort keys is not predictable.


Code:
10       implicit integer (A-Y)
11      dimension sxlist (8,P2)
12      X0=P2  #P2=1+number of entries
13  10 X0=X0/2
14      if (X0) 15,70,15
15  15 X1=P2-X0
16      X2=1
17  20 X3=X2
18  30 X4=X3+X0
19      if (sxlist(2,X3)-sxlist(2,X4)) 60,45,40
20  45 if (sxlist(1,X3)-sxlist(1,X4)) 60,60,40
21  40 do 50 I-1,8
22       X5=sxlist(I,X3)
23      sxlist(I,X3)=sxlist(I,X4)
24      sxlist(I,X4)=X5
25  50 continue
26     X3=X3-X0
27     if (X3-1) 60,60,30
28  60 X2=X2+1
29    if (X2-X1) 20,20,10
30  70 return


Previous Thread | Next Thread
Test Your Knowledge in Computers #127
Difficulty: Easy
A standard Linux distribution consists of a Linux kernel, the GNU system, GNU utilities, and other software; but no window manager.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort multidimensional Array

Hello I have a problem. I create a Multidimensional Array Like this: ENTRY="$kunnum-$host" ENTRY="$host" ENTRY="# $3" for key in "${!ENTRY}"; do ENTRIES=${ENTRY} # INDEX=IP(5) donedeclare -p declare -A ENTRIES=(="unas15533" ="unas" ="# RDP-Terminal 2"... (12 Replies)
Discussion started by: Marti95
12 Replies

2. Shell Programming and Scripting

Multidimensional array

I am learning about bash system variables, such as $ , @ and #. I have this piece of script implementing an array and it is doing its job just fine. This is not the only array I will be using. Just for ease of maintenance and more coding I would like to have the arrays in two dimensional... (4 Replies)
Discussion started by: annacreek
4 Replies

3. Shell Programming and Scripting

Multidimensional arrays Shell Programming and Scripting

I have two files: file-1 is a list of number of interfaces in the switch and file-2 have VLAN-ID , VLAN-NAME , Interface belong to that VLAN like this: file-1: 1/1 1/2 1/3 1/4 1/5 . . file-2: 1,"vlan-wifi",1/1,1/7,1/8 (9 Replies)
Discussion started by: SULTAN01
9 Replies

4. Shell Programming and Scripting

How do I sort two arrays using awk?

Hi How do I sort two arrays in a ascending order? I'm just doing this to teach myself a book my dad got me on unix. The arrays are A and B and both are including values taken from a file input by the user. How do I arrange the contents of both arrays so that they are both ordered from... (3 Replies)
Discussion started by: razrnaga
3 Replies

5. Shell Programming and Scripting

Multidimensional arrays

Hi Experts, I'm a newbie.....multidimensional arrays in shell scripts is possible??? If so, can anyone guide me with an example.....and also can anyone tell me, how we can create a table like ouput format in shell script....the output should look like this one: 1 2 3 4 5 6 6 5 4 3... (4 Replies)
Discussion started by: kritibalu
4 Replies

6. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

7. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

8. Shell Programming and Scripting

multidimensional arrays using awk

i'm trying to use awk to count a listing similar to the following and get a report of the listing similar to the one below it. y,pizza n,pizza y,pizza y,pizza n,tomato n,tomato y,cheese y,cheese n,cheese report ---- pizza,3,1 tomato,0,2 cheese,2,1 (1 Reply)
Discussion started by: multimulti
1 Replies

9. Shell Programming and Scripting

awk multidimensional values

Hello All, Here is my input file content and i am trying to write an awk file that produces the output as shown below. i tried with my little knowledge but was not successful. Input File content: ID COL1 COL2 COL3 1002 1 val21 val31 1002 2 val22 val32 1002 3 val23 val33... (2 Replies)
Discussion started by: forumthreads
2 Replies

10. Shell Programming and Scripting

multidimensional arrays

I have a file that's logically in the form of a multidimensional array with an unknown number of records in the file. The file looks like this: name1 data1 name2 data2 name3 data3 ... nameN dataN How do I load this file into an array for processing, while... (2 Replies)
Discussion started by: leslie02
2 Replies

Featured Tech Videos