Connecting Tech Pros Worldwide Help | Site Map

Sorting Multidimensional Array

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 27th, 2006, 12:45 AM
Index
Guest
 
Posts: n/a
Default Sorting Multidimensional 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.


  #2  
Old May 27th, 2006, 01:45 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

Index wrote:[color=blue]
> 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.[/color]

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.
  #3  
Old May 27th, 2006, 01:55 AM
Ian Collins
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

Ian Collins wrote:[color=blue]
>
> If you want greater efficiency, create a smart pointer type for Data
> with its own < operator and put these in the set.
>[/color]
I should have added - be sure to profile both options and don't bother
with the second unless you realy have to!

--
Ian Collins.
  #4  
Old May 27th, 2006, 10:35 AM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

On 26 May 2006 17:45:46 -0700, "Index" <kasturi.chatterjee@gmail.com>
wrote:[color=blue]
>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.[/color]

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
  #5  
Old May 27th, 2006, 09:05 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

Roland Pibinger wrote:[color=blue]
>
> 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.
>[/color]
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.
  #6  
Old May 27th, 2006, 10:05 PM
Phlip
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

Ian Collins wrote:
[color=blue]
> Roland Pibinger wrote:[color=green]
>>
>> 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.
>>[/color]
> vaguely?[/color]

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!!!


  #7  
Old May 28th, 2006, 10:45 AM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: Sorting Multidimensional Array

On Sat, 27 May 2006 21:59:35 GMT, "Phlip" <phlipcpp@yahoo.com> wrote:[color=blue]
>Arrays are not first class objects?
>
> template<class T, const size_t extent>
> size_t extent_of(T(&)[extent]) { return extent; }[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.