473,513 Members | 2,665 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: sort list base on another list

Tem
Thanks for all your responses. I see why c# is such a powerful language and
starting to like it more and more.

I also need help sorting this nested list. It is a little tricky. not sure
where to start.
I was thinking about creating a mapper for the nested lists.

List<stringstringList = new List<string>();
List<List<int>intLists = new List<List<int>>();

stringList.Add("c");
stringList.Add("b");
stringList.Add("a");

intLists.Add(new List<int>());
intLists.Add(new List<int>());
intLists.Add(new List<int>());

intLists[0].Add(3);
intLists[0].Add(1);
intLists[0].Add(2);

intLists[1].Add(7);
intLists[1].Add(8);
intLists[1].Add(2);

intLists[2].Add(0);
intLists[2].Add(9);
intLists[2].Add(9);

Output would be-
a,b,c
2,1,3
2,8,7
9,9,0

Any suggestions?

Tem

Jun 27 '08 #1
6 4845
On Sat, 03 May 2008 23:43:17 -0700, Tem <te*****@yahoo.comwrote:
[...]
I also need help sorting this nested list. It is a little tricky. not
sure where to start.
[...]
Any suggestions?
Yes. Stop making these parallel data structures. :)

Seriously though...

The approach I suggested earlier about having a separate integer list
that's used as an index is probably the most straight-forward approach,
barring fixing the data structure so it's not so clumsy. I wasn't aware
of the Array.Sort() overload Arne mentioned, and that seemed like the
perfect solution to your specific problem as stated earlier. But if you
have more than two correlated lists, that's not going to do it. A sorted
index list would, however.

Pete
Jun 27 '08 #2
Tem
Im not sure how to build such sorted index list.

SortedList<string, intlist = new SortedList<string, int>();

is this what you are referring?

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sat, 03 May 2008 23:43:17 -0700, Tem <te*****@yahoo.comwrote:
>[...]
I also need help sorting this nested list. It is a little tricky. not
sure where to start.
[...]
Any suggestions?

Yes. Stop making these parallel data structures. :)

Seriously though...

The approach I suggested earlier about having a separate integer list
that's used as an index is probably the most straight-forward approach,
barring fixing the data structure so it's not so clumsy. I wasn't aware
of the Array.Sort() overload Arne mentioned, and that seemed like the
perfect solution to your specific problem as stated earlier. But if you
have more than two correlated lists, that's not going to do it. A sorted
index list would, however.

Pete
Jun 27 '08 #3
Tem wrote:
Thanks for all your responses. I see why c# is such a powerful language
and starting to like it more and more.

I also need help sorting this nested list. It is a little tricky. not
sure where to start.
I was thinking about creating a mapper for the nested lists.

List<stringstringList = new List<string>();
List<List<int>intLists = new List<List<int>>();

stringList.Add("c");
stringList.Add("b");
stringList.Add("a");

intLists.Add(new List<int>());
intLists.Add(new List<int>());
intLists.Add(new List<int>());

intLists[0].Add(3);
intLists[0].Add(1);
intLists[0].Add(2);

intLists[1].Add(7);
intLists[1].Add(8);
intLists[1].Add(2);

intLists[2].Add(0);
intLists[2].Add(9);
intLists[2].Add(9);

Output would be-
a,b,c
2,1,3
2,8,7
9,9,0
This basicly the same problem. You sort the string list and you
similar sort 3 int lists (intLists[0], intLists[1] and intLists[2]).

You can do it.

But you should seriously consider restructuring the data.

Arne
Jun 27 '08 #4
Tem
Do you have any suggestions on how I can better structure the data?
It needs to have a flexible list size where lists can be added or removed.

"Arne Vajhj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
Tem wrote:
>Thanks for all your responses. I see why c# is such a powerful language
and starting to like it more and more.

I also need help sorting this nested list. It is a little tricky. not
sure where to start.
I was thinking about creating a mapper for the nested lists.

List<stringstringList = new List<string>();
List<List<int>intLists = new List<List<int>>();

stringList.Add("c");
stringList.Add("b");
stringList.Add("a");

intLists.Add(new List<int>());
intLists.Add(new List<int>());
intLists.Add(new List<int>());

intLists[0].Add(3);
intLists[0].Add(1);
intLists[0].Add(2);

intLists[1].Add(7);
intLists[1].Add(8);
intLists[1].Add(2);

intLists[2].Add(0);
intLists[2].Add(9);
intLists[2].Add(9);

Output would be-
a,b,c
2,1,3
2,8,7
9,9,0

This basicly the same problem. You sort the string list and you
similar sort 3 int lists (intLists[0], intLists[1] and intLists[2]).

You can do it.

But you should seriously consider restructuring the data.

Arne
Jun 27 '08 #5
Tem wrote:
Do you have any suggestions on how I can better structure the data?
It needs to have a flexible list size where lists can be added or removed.
I think you need a class that contains a string and a List<int>.

Private fields and public properties ofcourse.

And implementing the IComparer<stringinterface that allows
for natural sorting.

Arne
Jun 27 '08 #6
On Sun, 04 May 2008 20:16:26 -0700, Tem <te*****@yahoo.comwrote:
Do you have any suggestions on how I can better structure the data?
It needs to have a flexible list size where lists can be added or
removed.
I don't really understand what you mean by "where lists can be added or
removed". That's too vague a statement to be meaningful.

However, looking at your most recent example, you could define a class
like this:

class A
{
string str;
List<intlistInts;
}

Where the field "str" contains the element that would have been in your
stringList, and the listInts list contains the respective elements that
would have been in each of your intLists at the appropriate position for
the string.

As Arne says, the actual class would be declared more appropriately, with
private fields, public properties, etc.

Pete
Jun 27 '08 #7

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

Similar topics

20
4023
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
4
2154
by: DancnDude | last post by:
I have a class that needs to have several different kinds of sorting routines on an ArrayList that it needs to conditionally do based upon the data. I have successfully created a class that...
10
15100
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
3
3046
by: Ed Sutton | last post by:
I need to do a custom sort on a TreeView. I have various object types associated with the TreeNode Tag property. I want to sort objects of the same type at the top of the list, other objects at...
0
1725
by: joseph speigle | last post by:
hi, To see the query results in native language see http://database.sarang.net/?inc=read&aid=5368&criteria=pgsql&subcrit=qna&id=&limit=20&keyword=&page=1 the simpler url is ...
21
3169
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
22
6241
by: AB | last post by:
Hello All, I'm trying to replicate a general purpose sort function (think qsort) void sort(void *arr, const int num, size_t size, int (*cmp)(void *a, void *b)) { int i = 0 ; int j = 0 ;
18
2314
by: xahlee | last post by:
Last year, i've posted a tutorial and commentary about Python and Perl's sort function. (http://xahlee.org/perl-python/sort_list.html) In that article, i discussed a technique known among...
0
258
by: Marc Gravell | last post by:
To illustrate Peter's point - here is the code (using C# 3 for brevity, but only .NET 2.0 - no LINQ etc) to do this using an entity (SomeType) to represent the combined values: Marc using...
0
7379
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,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
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...
0
5682
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 projectplanning, coding, testing,...
0
4745
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.