473,791 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1729
In article <11************ **********@m73g 2000cwd.googleg roups.com>,
"Noah Roberts" <ro**********@g mail.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**********@g mail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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**********@g mail.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.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_mod e_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.supernew s.com>,
Andrey Tarasevich <an************ **@hotmail.comw rote:
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_mod e_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
2533
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() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
0
2736
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 not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
12
5678
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 this is not the most efficient method. Ideally, I'd like to pass a parameter to the SP to indicate sorting field order... How do you guys go about this?
2
1901
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 above a particular column). What I need to know now is whether this is possible with XML/XSL? Or do I need to resort to a programming language (maybe JScript?). So far I have worked with the XML and XSL split into separate documents. However, I...
8
3539
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
2
1863
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 double-clicked - it will be sorted DESC It is not exactly toggling the sorting-order. I have seen a lot of messages and articles in the web about toggling the sort order (like first time click
2
6178
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 ... http://www.bayes.co.uk/xml/portal.aspx?page=/xml/tutorial/index.xml&subpage=/xml/tutorial/filtering/filter.xml I experimented with the examples and found that they would run fine on a local PC (without installing a web-server). That is just what I need!
6
2495
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 I want is when someone clicks a title chooses the sorting option, if this is the actual sorting option then reverses the order. Can I do this? How?
2
2710
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 it. I started out using Prototype to help with the sorting, but it appears that this results in a lot of calls to _getElementsByXPath, which are taking up a large percentage of the rendering time. I believe this call is occurring as the DOM is...
5
4960
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 there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
9669
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
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
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
9995
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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
7537
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
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5431
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...
2
3718
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.