Sign in to follow this  
Salvinger

[.net] Sorting a parallel collection/array

Recommended Posts

Salvinger    122
So I've recently needed to sort a collection (more specifically a listviewitem collection) and an array at the same time, but at the same time keeping them parallel. I wanted to use the collection's built in sort feature, however, the most I've found I can do with it is return a value from a comparison of two objects in the collection. In other words, the collection's sorting method seems to be totally internal. Normally that is fine, but I want to keep the collection's elements and the array's elements parallel so I would need to know each time the collection switches some elements so that I can do the same in the array. My question is, is there an event or something that is used when elements of the collection are moved around, or am I going to have to write a sort that sorts both at the same time?

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Create an array containing integers 0..Length-1. Use the comparison operator defined as "i < j iff data[i] < data[j]". Sort that array.

The result is an array containing at position i the index of the element in the original container that should be at position i in the new container. So, basically, newdata[i] = data[array[i]] sorts "newdata" from "data".

Apply this array to both containers in order to have them both sorted.

Share this post


Link to post
Share on other sites
paic    645
Hi,
I don't really know how to do this, but it reminds me of a piece of code in my program : I have a ListView in which the ListViewItems store some data. The ListViewItem is just here to expose the data to the user.

What I did is, instead of storing my data in an array, and maintain the ListViewItems in parallel is this :

public ref class CustomItem : public ListViewItem
{
// simply put the data / properties you need here
private int m_Example = 1;
public property int Example { get { return(m_Example); } }
};




Then you can add instances of CustomItem to your ListView, you can even override the ToString() method to display what you want in the ListView (for example, updating the displayed text when the data change) you can sort your data without bothering with another list, etc.

And to access your data :

foreach (CustomItem item in listView1.Items)
{
int example = item.Example;
}




I don't know if you can do it in your program, but for me it was far more simpler to do that than maintain 2 separate lists.


PS: I'm using .NET with managed C++, but since most people use C#, I tried to write C# code, I hope I didn't make mistakes :)

Share this post


Link to post
Share on other sites
Salvinger    122
Thanks for both of your guy's suggestions, right now I'm actually using the listviewitem's tag property to store the structure associated with it, so it would be similar to the suggestion you posted, but I'm trying to figure out if it would be more efficient to search and going through each listviewitem and checking it's tag and sorting it that way, or keeping two parallel arrays

Share this post


Link to post
Share on other sites
Nypyren    12074
I've had similar architecture with some of my projects, and I've been thinking about making some kind of generic 'composite container' which groups multiple sub-containers and performs operations on them composite-style to keep them in sync. It might be overkill for your purposes, but you might give it some thought.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this