For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > Implement ORDER BY clause in c++









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Implement ORDER BY clause in c++
Index

2006-05-26, 7:09 pm

Hi,
I have a file which I want to sort depending on multiple columns.
Actually I want to implement the following query through programming:

SELECT SUM(VALUES) GROUP BY x,y ORDER BY x,y;

the aggregation is complete.What I want is to order the result.I have 7
fields which are used in GROUP BY clause and I need to ORDER them with
those fields.
Any help will be highly appreciated.

Mark Randall

2006-05-26, 7:09 pm

Use a bog standard quicksort or bubblesort on what (we presume) has been
read into some form of data structure

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."

"Index" <kasturi.chatterjee@gmail.com> wrote in message
news:1148688619.487742.22340@g10g2000cwb.googlegroups.com...
> Hi,
> I have a file which I want to sort depending on multiple columns.
> Actually I want to implement the following query through programming:
>
> SELECT SUM(VALUES) GROUP BY x,y ORDER BY x,y;
>
> the aggregation is complete.What I want is to order the result.I have 7
> fields which are used in GROUP BY clause and I need to ORDER them with
> those fields.
> Any help will be highly appreciated.
>



Index

2006-05-26, 10:05 pm

Actually, teh scenario is a multi key srting I guess.To explain the
situation, please go thru the example.


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.

How can I modify bubble sort or any other standard sorting algo to
perform this?
Thanks.

Tamas Demjen

2006-05-26, 10:05 pm

Index wrote:

> I have a file which I want to sort depending on multiple columns.


It depends where your data are located, and how they are represented. If
your records fit in the memory, you could store them in an STL vector,
then define a custom ordering operator.

Here's how to create a function object that orders by two fields (first
by "priority", and if they are the same, then by "size"):

struct Record
{
int priority;
int size;
[...] // other members
};

struct OrderByPriorityThenSize
{
bool operator()(const Record& lhs, const Record& rhs) const
{
if(lhs.priority == rhs.priority)
return lhs.size < rhs.size; // secondary ordering
else
return lhs.priority < rhs.priority; // primary ordering
}
};

Then assuming you have a vector of Record's, you can sort them using
your defined order:

std::vector<Recrod> items;
[...] // load the items
std::sort(items.begin(), items.end(), OrderByPriorityThenSize());
[...] // save the items

There are some ultra lightweight database engines available, such as
SQLite. That way you can use SQL queries to manage your data.

I'm not sure if this answers your question. If not, you'll need to
provide more details about your project.

Tom
Scott McPhillips [MVP]

2006-05-26, 10:05 pm

Index wrote:
> 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.


std::sort is a highly optimized sorting algorithm for a std::vector. If
you use a function object you can even use one comparison routine for
any column(s). See a book on STL (Standard Template Library) for
enlightenment.

--
Scott McPhillips [VC++ MVP]

Index

2006-05-28, 7:13 pm

Hi,
Thanks for all the inputs.But what I understand from all this that at
any instant, the std:sort uses only one key to sort.But I need multiple
key sorting for each instant where ther combination of teh keys is
dynamic.Algorithmically, i devised something like this, but i don't
think it is efficient.Can I use any STL method to achieve the same?

say for a record in a vector, two keys x and y are to be used to sort
the file where the file has been already aggregated using A and then
B.[same as SELECT SUM(VALUES) GROUP BY x,y]...I already implemented
this portion.Now I need to ORDER them by x and then by y.I thought of
implementing the order by as follows:

First sort by x;
Then sort by y depending on the folllowing condition while swapping the
elements:
if (Vector[i]->x == Vector[i+1]->x && Vector[i]->y > Vector[i+1]->y)
{
SWAP (Vector[i] <-> Vector[i+1]);
}

Now I have 5 columns which can be the keys in various combination an
dthe sorting should be done accordingly.There will be five different
sorting.

say for 3 keys x,y and z, it will be as follows:
First sort by x;
Then sort by y depending on the folllowing condition while swapping the
elements:
if (Vector[i]->x == Vector[i+1]->x && Vector[i]->y > Vector[i+1]->y)
{
SWAP (Vector[i] <-> Vector[i+1]);
}
Then sort by z depending on the folllowing condition while swapping the
elements:
if (Vector[i]->x == Vector[i+1]->x && Vector[i]->y == Vector[i+1]->y &&
Vector[i]->z > Vector[i+1]->z)
{
SWAP (Vector[i] <-> Vector[i+1]);
}


I am really not very convinced this is a very good idea and I think
there can be better ways to do so.
Please do let me know of any idea.
Thanks.

Index

2006-05-28, 7:13 pm

sorry for the quick reply...i read through the STL and Strict Weak
Ordering properly, and i think the solution posted by Tamas Demjen will
solve my problem as essentially it implments the same logic that I
devised naively:)...please do correct me if I am wrong.
The key fields are of different data types like most of them are
strings and one is integer.I have a simple question:
Does something like this [lhs.size < rhs.size] work for strings and
integers in the same fashion?

Mark Randall

2006-05-28, 7:13 pm

"Index" <kasturi.chatterjee@gmail.com> wrote in message
news:1148858451.317452.90780@y43g2000cwc.googlegroups.com...
> sorry for the quick reply...i read through the STL and Strict Weak
> Ordering properly, and i think the solution posted by Tamas Demjen will
> solve my problem as essentially it implments the same logic that I
> devised naively:)...please do correct me if I am wrong.
> The key fields are of different data types like most of them are
> strings and one is integer.I have a simple question:
> Does something like this [lhs.size < rhs.size] work for strings and
> integers in the same fashion?


Only if they have an operator< of the same type.

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."


Index

2006-05-28, 7:13 pm

can you please elaborate a little bit please?

Tamas Demjen

2006-05-31, 7:15 pm

Index wrote:

> Does something like this [lhs.size < rhs.size] work for strings and
> integers in the same fashion?


Are you using std::string? Yes, it has an operator<, and it's a pure
ASCII, case sensitive, culture independent comparison (doesn't respect
the currently selected language in Windows). You might want to consider
the CompareString function, or the locale features in the STL.

Tom
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com