473,800 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting Multidimensiona l Array

Hi, I have a 4 dimensional array and I want to sort it by the first two
columns emulating an ORDER BY clause to some extent.Any suggestion?

The original Array is:

Row1-> 4 8 2 9
Row2-> 4 5 5 3
Row3-> 3 9 8 7
Row4-> 8 2 4 8

The sorted array should look:

Row1-> 3 9 8 7
Row2-> 4 5 5 3
Row3-> 4 8 2 9
Row4-> 8 2 4 8

In fact the columns upon which the sorting is to be done is also
dynamic.
Please provide me some efficient algo or implementation. The data to be
handled is rather huge.So efficiency is a vital requirement.
Thanks.

May 27 '06 #1
6 3542
Index wrote:
Hi, I have a 4 dimensional array and I want to sort it by the first two
columns emulating an ORDER BY clause to some extent.Any suggestion?

The original Array is:

Row1-> 4 8 2 9
Row2-> 4 5 5 3
Row3-> 3 9 8 7
Row4-> 8 2 4 8

The sorted array should look:

Row1-> 3 9 8 7
Row2-> 4 5 5 3
Row3-> 4 8 2 9
Row4-> 8 2 4 8

In fact the columns upon which the sorting is to be done is also
dynamic.
Please provide me some efficient algo or implementation. The data to be
handled is rather huge.So efficiency is a vital requirement.


Store your data in a struct and define a < operator for the struct based
on your sort rules. Then shove the data structs into a set.

Something like:

struct Data
{
int a, b, c, d, e;

bool operator<( const Data& ) const;
};

bool Data::operator< ( const Data& data ) const
{
return (a < data.a) ? true : (a == data.a) ? data.b > b : false;
}

If you want greater efficiency, create a smart pointer type for Data
with its own < operator and put these in the set.

--
Ian Collins.
May 27 '06 #2
Ian Collins wrote:

If you want greater efficiency, create a smart pointer type for Data
with its own < operator and put these in the set.

I should have added - be sure to profile both options and don't bother
with the second unless you realy have to!

--
Ian Collins.
May 27 '06 #3
On 26 May 2006 17:45:46 -0700, "Index" <ka************ ****@gmail.com>
wrote:
Hi, I have a 4 dimensional array and I want to sort it by the first two
columns emulating an ORDER BY clause to some extent.Any suggestion?

The original Array is:

Row1-> 4 8 2 9
Row2-> 4 5 5 3
Row3-> 3 9 8 7
Row4-> 8 2 4 8

The sorted array should look:

Row1-> 3 9 8 7
Row2-> 4 5 5 3
Row3-> 4 8 2 9
Row4-> 8 2 4 8

In fact the columns upon which the sorting is to be done is also
dynamic.
Please provide me some efficient algo or implementation. The data to be
handled is rather huge.So efficiency is a vital requirement.
Thanks.


It's not possible to use qsort or std::sort directly because arrays
are not first class objects in C/C++. You should use a struct wrapper
for your Rows as vaguely indicated by Ian Collins.

Best wishes,
Roland Pibinger
May 27 '06 #4
Roland Pibinger wrote:

It's not possible to use qsort or std::sort directly because arrays
are not first class objects in C/C++. You should use a struct wrapper
for your Rows as vaguely indicated by Ian Collins.

vaguely?

"Store your data in a struct and define a < operator for the struct
based on your sort rules."

I guess I could have shouted :)

--
Ian Collins.
May 27 '06 #5
Ian Collins wrote:
Roland Pibinger wrote:

It's not possible to use qsort or std::sort directly because arrays
are not first class objects in C/C++. You should use a struct wrapper
for your Rows as vaguely indicated by Ian Collins.

vaguely?


Arrays are not first class objects?

template<class T, const size_t extent>
size_t
extent_of(T(&)[extent]) { return extent; }

RP meant that arrays are tricky to use as first class objects, and that
those sorters require a wrapper...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 27 '06 #6
On Sat, 27 May 2006 21:59:35 GMT, "Phlip" <ph******@yahoo .com> wrote:
Arrays are not first class objects?

template<class T, const size_t extent>
size_t extent_of(T(&)[extent]) { return extent; }


I don't know what you want to demonstrate. The original problem,
sorting a 2-dimensional array (preferably with Standard facilities
like sort or qsort), is still unresolved in the currend thread.

Best regards,
Roland Pibinger
May 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
13663
by: Brian | last post by:
I have an array like this: $events = array( array( '2003-07-01', 'Event Title 1', '1' //ID Number (not unique) ), array( '2003-07-02',
2
2010
by: markus | last post by:
I usually store info in arrays using field names $array='Germany', $array="GE"; $array='United Sates" $array="US"; $array='France" $array='FR"
2
2652
by: MM | last post by:
Hi there, Is it at all possible to sort the contents of a table (ordinary html <table><tr><td>...</td></tr></table>) using javascript? What I want is to be able to select, for example by klicking a button or checking a radio button, whether the table shall be sorted by column 1 och 2. Even if the table has three or more columns I'm only interested in sorting (increasingly) by first or second. I would be nice if I did not have to...
1
2146
by: Gaspard Kayitare | last post by:
I would like to know how to use multidimensional arrays principles for sorting grades for four students obtained in four subjects.I am supposed to arrange the data in a form of a table,students names by rows and subjects/course units by columns.How should I organise my c++ code.The program should sort the grades of each students in ascending or descending orders.
9
2617
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
16
3627
by: Rehceb Rotkiv | last post by:
Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb
1
1757
pradeepjain
by: pradeepjain | last post by:
Hii, i need help in sorting multidimensianal array... i Read the multi sort function cld not use tht so i need help..This is the code. here the problem is tht i need to take out values from 2 different tables.i am getting the output properly but only thing is tht i need to sort the o/p as per name here fid=6 is designation..uid is the id of the user ,fid=3 is Name of the person,fid=11 is for extension ,fid=1 is research...
7
7024
pradeepjain
by: pradeepjain | last post by:
Hello Guys, I am using a multidimensional array in which there is a date field also..i want to sort the multidimensional array based on this date filed how to do this. Thanks, Pradeep
9
4503
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize function: x = new int;
0
9691
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
10507
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...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
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,...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
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
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4150
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
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.