473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sorting side by side

I know there must be a way to do this using stl functions and not
reinventing the sort loops and such but I need to sort several arrays
side by side using one as the "key". Interested in ideas here...

Two directions I can think of would be to create some sort of iterator
or to create an object to wrap the value and index of the key array and
then place these in a vector and sort it. Creating an iterator seems
like maybe too complex but would probably be the more efficient
way...maybe it could be generalized enough to use again sometime. It
would certainly teach me more about iterators as I have never built my
own; always use what is there already. The other would be easier,
maybe.

I'm very open to suggestion here.

Jul 14 '06 #1
7 1695
In article <11**********************@m73g2000cwd.googlegroups .com>,
"Noah Roberts" <ro**********@gmail.comwrote:
I know there must be a way to do this using stl functions and not
reinventing the sort loops and such but I need to sort several arrays
side by side using one as the "key". Interested in ideas here...

Two directions I can think of would be to create some sort of iterator
or to create an object to wrap the value and index of the key array and
then place these in a vector and sort it. Creating an iterator seems
like maybe too complex but would probably be the more efficient
way...maybe it could be generalized enough to use again sometime. It
would certainly teach me more about iterators as I have never built my
own; always use what is there already. The other would be easier,
maybe.

I'm very open to suggestion here.
Create a class that contains one of each element from each array, they
are obviously dependent. Make a container of these objects and sort it.
Jul 14 '06 #2

"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I know there must be a way to do this using stl functions and not
reinventing the sort loops and such but I need to sort several arrays
side by side using one as the "key". Interested in ideas here...

Two directions I can think of would be to create some sort of iterator
or to create an object to wrap the value and index of the key array and
then place these in a vector and sort it. Creating an iterator seems
like maybe too complex but would probably be the more efficient
way...maybe it could be generalized enough to use again sometime. It
would certainly teach me more about iterators as I have never built my
own; always use what is there already. The other would be easier,
maybe.

I'm very open to suggestion here.
One way is to make a std::vector<int(call it "index") the same length as
the arrays and sort it so that some_array[index[j]] is in the proper order.
Then the other arrays can be accessed in order using the same trick. If you
need to actually reorder all the arrays that becomes trivial once you have
the index array.

BTW it may be a symptom of a design problem if you have a bunch of parallel
arrays. Why not refactor into a single array of objects? Of course I know
that sometimes there are preconditions of the job which prevent this.

Cy
Jul 14 '06 #3

Cy Edmunds wrote:
"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
I know there must be a way to do this using stl functions and not
reinventing the sort loops and such but I need to sort several arrays
side by side using one as the "key". Interested in ideas here...

Two directions I can think of would be to create some sort of iterator
or to create an object to wrap the value and index of the key array and
then place these in a vector and sort it. Creating an iterator seems
like maybe too complex but would probably be the more efficient
way...maybe it could be generalized enough to use again sometime. It
would certainly teach me more about iterators as I have never built my
own; always use what is there already. The other would be easier,
maybe.

I'm very open to suggestion here.

One way is to make a std::vector<int(call it "index") the same length as
the arrays and sort it so that some_array[index[j]] is in the proper order.
Then the other arrays can be accessed in order using the same trick. If you
need to actually reorder all the arrays that becomes trivial once you have
the index array.
I was thinking this wouldn't work because there would be no way to tie
the int with the subject but by providing a comp function this should
work. Simpler than the wrapper.
>
BTW it may be a symptom of a design problem if you have a bunch of parallel
arrays. Why not refactor into a single array of objects? Of course I know
that sometimes there are preconditions of the job which prevent this.
Yes, there rather are. They are public members of a class and accessed
all over the damn place. It's still tempting to fix it though.

Jul 14 '06 #4
Noah Roberts wrote:
>...
One way is to make a std::vector<int(call it "index") the same length as
the arrays and sort it so that some_array[index[j]] is in the proper order.
Then the other arrays can be accessed in order using the same trick. If you
need to actually reorder all the arrays that becomes trivial once you have
the index array.

I was thinking this wouldn't work because there would be no way to tie
the int with the subject but by providing a comp function this should
work. Simpler than the wrapper.
Instead of an 'int' array you can sort a 'std::vector<T*>' array, where
'T' is the element type in the index array. This solves the tying issue
in the most straightforward way.

Once the pointer vector is sorted, it is easy to rearrange all the
arrays. (Note, that you can easily convert a 'T*' pointer into the array
index using address arithmetics).

--
Best regards,
Andrey Tarasevich
Jul 14 '06 #5
Cy Edmunds wrote:
...
BTW it may be a symptom of a design problem if you have a bunch of parallel
arrays. Why not refactor into a single array of objects? Of course I know
that sometimes there are preconditions of the job which prevent this.
...
It is not necessarily a design problem at all. When related data from
the same design level ends up in parallel arrays - that would most
likely indicate a problem. However, when different arrays belong to
completely different levels of design (i.e. when the data in the
additional arrays has absolutely no meaning in the context where the
original array is introduced), it is not a problem. Quite the opposite,
in that case putting it all in the same array would be a design error.
It is the later design error that is seen quite often in actual code.
Like, for example, a 'Tree_Node' class in a binary tree library, which
contains a 'mikes_file_mode_bitfield' and 'jeffs_pointer_to_the_io_pipe'
just because somewhere at more specific level of design Mike or Jeff
wanted to associate these pieces of data with the tree nodes and decided
that it would be nice to "have it in the same array".

--
Best regards,
Andrey Tarasevich
Jul 14 '06 #6
In article <12*************@news.supernews.com>,
Andrey Tarasevich <an**************@hotmail.comwrote:
Cy Edmunds wrote:
...
BTW it may be a symptom of a design problem if you have a bunch of parallel
arrays. Why not refactor into a single array of objects? Of course I know
that sometimes there are preconditions of the job which prevent this.
...

It is not necessarily a design problem at all. When related data from
the same design level ends up in parallel arrays - that would most
likely indicate a problem. However, when different arrays belong to
completely different levels of design (i.e. when the data in the
additional arrays has absolutely no meaning in the context where the
original array is introduced), it is not a problem.
Unfortunately, the condition of the question precludes that as a
possibility. The two arrays are intimately related and therefore must
both have meaning in the same context, or the problem of having to keep
them in the same order wouldn't have come up.
Quite the opposite,
in that case putting it all in the same array would be a design error.
It is the later design error that is seen quite often in actual code.
Like, for example, a 'Tree_Node' class in a binary tree library, which
contains a 'mikes_file_mode_bitfield' and 'jeffs_pointer_to_the_io_pipe'
just because somewhere at more specific level of design Mike or Jeff
wanted to associate these pieces of data with the tree nodes and decided
that it would be nice to "have it in the same array".
There is a dramatic difference between "wouldn't it be nice if they were
in the same array" and "They must be 'side by side' in order for the
system to work."
Jul 14 '06 #7

Noah Roberts wrote:
I know there must be a way to do this using stl functions and not
reinventing the sort loops and such but I need to sort several arrays
side by side using one as the "key". Interested in ideas here...

Two directions I can think of would be to create some sort of iterator
or to create an object to wrap the value and index of the key array and
then place these in a vector and sort it. Creating an iterator seems
like maybe too complex but would probably be the more efficient
way...maybe it could be generalized enough to use again sometime. It
would certainly teach me more about iterators as I have never built my
own; always use what is there already. The other would be easier,
maybe.
If you have two equal-sized arrays, one of keys and the other of
values, with the keys "linked" to the values conceptually, then
you're doing it all wrong. You're trying to re-define std::map.
Why not use a real std::map instead? It does all the sorting for
you, automatically. Each time you insert a new entry, it gets
added to the map in sorted position.

Research std::map and std::pair for more info.

Some sample code (pretested, working) to give you the idea:

#include <iostream>
#include <utility>
#include <map>

using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
using std::make_pair;

class MyClass
{
public:
MyClass() : msg("") {}
MyClass(string const & Msg) : msg(Msg) {}
string msg;
};

int main()
{
typedef unsigned long int user_id;

map<user_id, MyClassfizzbin;

MyClass Bob ("Bob");
MyClass Tom ("Tom");
MyClass Sam ("Sam");
MyClass Fred ("Fred");

fizzbin.insert(make_pair(37307, Bob));
fizzbin.insert(make_pair(82219, Tom));
fizzbin.insert(make_pair(73884, Sam));
fizzbin.insert(make_pair(21984, Fred));

// At this point, all entries are automatically sorted
// in order of user id. Lookup is blazingly fast because
// it's implemented using binary-tree technology:

// Look-up and print name of user with id# 73884:
cout << fizzbin[73884].msg << endl;
}
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 15 '06 #8

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

Similar topics

4
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()...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
12
by: CJM | last post by:
How can I dynamically sort the results from a Stored Procedure? Or more importantly, what is the fastest and most efficient way? I know I can do the sorting within the recordset in ASP, but AFAIK...
2
by: Alan Searle | last post by:
I find that I can display structured data very nicely using XML with an XSL template. As an extra 'goodie', I would like to give users the ability to sort that data (for example with a button...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
2
by: Snig | last post by:
Hi, I want to sort the DataGrid according to the number of clicks of mouse on the column-header link. e.g. if user clicks on the header once - it will be sorted ASC if the header is...
2
by: Alan Searle | last post by:
I would like to use an XSL/HTML template to sort XML data dynamically on the client side. As it is, I found a tutorial showing how to do this on the following site ... ...
6
by: bcochofel | last post by:
I'm using xsl to list an xml file that contains something like: sites, tag and weight. I'm listing this in a table with the following titles: | URL | TAG | WEIGHT (each title his a link) What...
2
by: Simon | last post by:
Hi, I have a table I am populating with data from a DB, and have it sortable on the client side. When the table exceeds around 300 rows, I start running into problems with rendering and sorting...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...

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.