473,671 Members | 2,252 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<stringstri ngList = new List<string>();
List<List<int>i ntLists = new List<List<int>> ();

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

intLists.Add(ne w List<int>());
intLists.Add(ne w List<int>());
intLists.Add(ne w 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 4858
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<stri ng, intlist = new SortedList<stri ng, int>();

is this what you are referring?

"Peter Duniho" <Np*********@nn owslpianmk.comw rote 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<stringstri ngList = new List<string>();
List<List<int>i ntLists = new List<List<int>> ();

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

intLists.Add(ne w List<int>());
intLists.Add(ne w List<int>());
intLists.Add(ne w 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.d kwrote in message
news:48******** *************** @news.sunsite.d k...
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<stringstri ngList = new List<string>();
List<List<int>i ntLists = new List<List<int>> ();

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

intLists.Add(ne w List<int>());
intLists.Add(ne w List<int>());
intLists.Add(ne w 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<strin ginterface 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<intlistInt s;
}

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
4055
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: li=;
4
2162
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 implements IComparer and does the sort correctly for one of these sort types. This class's definition is like: // Sort class that implements IComparer so public class CompareFields1 : IComparer { public int Compare(object a, object b)
10
15121
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
3
3052
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 the bottom. I found some scraps of code in various postings that I am trying to get to work. I can not seem to get any meaningful data back from the CompareFunc callback. According to MSDN, The lParam1 and lParam2 parameters correspond to the...
0
1740
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 http://database.sarang.net/?criteria=pgsql becase there is no korean postgresql list. poster is "joesp"
21
3199
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
22
6274
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
2333
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 juvenile Perlers as the Schwartzian Transform, which also manifests in Python as its “key” optional parameter. Here, i give a more detailed account on why and how of this construct. ----
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 System; using System.Collections.Generic; sealed class SomeType
0
8476
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8393
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
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7437
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, and deploymentwithout 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
6229
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
5696
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
4225
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
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.