473,375 Members | 1,321 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,375 software developers and data experts.

Complex sorting problem with List<>

I need some help with a multilevel sorting problem with the List<>.

I have a List<ItemToSort( see below ) that needs to be sorted in the
following manner:
Sort by Level1Id ( ok that was the easy part)
Within the unique Level1Id's, sort by Level2Id
Within the unique level2Id's sort by name, then description.

I looked at doing this with some foreach and list.FindAll() and copying the
list into a new list, but this had some code smells. I know there must be a
better solution than making many copies of a list.

The true code is much more complex, and uses custom Comparers, but this
would illustrate the problem. Actually, if I get to the third level (
remember the first one was ez), I can take it from there.

public class ItemToSort
{
public int Level1Id;
public string Level2Id;
public string Name;
public string Description;
}
Not necessarily looking for the code, but some guidlines.
Thanks!

Feb 7 '07 #1
4 3477
use a comparitor and pass it to Sort, with the comparitor something like:

int myCompare(Item first, Item second)
{
if (first.level1id != second.level1.id)
return first.level1id.CompareTo(second.level1id);
if (first.level2id != second.level2id)
return first.level2id.CompareTo(second.level2id);
if (first.Name != second.Name)
return first.Name.CompareTo(second.Name);
return first.Description.CompareTo(second.Description)
}

"LarryR" <La****@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
>I need some help with a multilevel sorting problem with the List<>.

I have a List<ItemToSort( see below ) that needs to be sorted in the
following manner:
Sort by Level1Id ( ok that was the easy part)
Within the unique Level1Id's, sort by Level2Id
Within the unique level2Id's sort by name, then description.

I looked at doing this with some foreach and list.FindAll() and copying
the
list into a new list, but this had some code smells. I know there must be
a
better solution than making many copies of a list.

The true code is much more complex, and uses custom Comparers, but this
would illustrate the problem. Actually, if I get to the third level (
remember the first one was ez), I can take it from there.

public class ItemToSort
{
public int Level1Id;
public string Level2Id;
public string Name;
public string Description;
}
Not necessarily looking for the code, but some guidlines.
Thanks!

Feb 8 '07 #2
Maybe having your object implement IComparable to reflect your sort
order is all you have to do.

public class ItemToSort : IComparable
{
public int Level1Id;
public string Level2Id;
public string Name;
public string Description;

#region IComparable Members

public int CompareTo(object obj)
{
if(obj == null)
return 1;

ItemToSort obj1 = obj as ItemToSort;

int c = this.Level1Id obj1.Level1Id ? 1 : (this.Level1Id
== obj1.Level1Id ? 0 : -1);
if (c == 0)
{
c = string.Compare(this.Level2Id, obj1.Level2Id);
}
if (c == 0)
{
c = string.Compare(this.Name, obj1.Name);
}
if (c == 0)
{
c = string.Compare(this.Description,
obj1.Description);
}
return c;
}

#endregion
}
===============
Clay Burch
Syncfusion, Inc.

Feb 8 '07 #3
Even better (IMO), use

public class ItemToSort : IComparable<ItemToSort>
{
public int CompareTo(ItemToSort other)
{
}
}

Since the OP uses a List<The won't have to worry about sorting against
non ItemToSort objects
On Thu, 08 Feb 2007 02:25:45 +0100, ClayB <cl***@syncfusion.comwrote:
Maybe having your object implement IComparable to reflect your sort
order is all you have to do.

public class ItemToSort : IComparable
{
public int Level1Id;
public string Level2Id;
public string Name;
public string Description;

#region IComparable Members

public int CompareTo(object obj)
{
if(obj == null)
return 1;

ItemToSort obj1 = obj as ItemToSort;

int c = this.Level1Id obj1.Level1Id ? 1 : (this.Level1Id
== obj1.Level1Id ? 0 : -1);
if (c == 0)
{
c = string.Compare(this.Level2Id, obj1.Level2Id);
}
if (c == 0)
{
c = string.Compare(this.Name, obj1.Name);
}
if (c == 0)
{
c = string.Compare(this.Description,
obj1.Description);
}
return c;
}

#endregion
}
===============
Clay Burch
Syncfusion, Inc.


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 8 '07 #4
Thanks! That did it nicely !
Feb 9 '07 #5

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

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
0
by: Marc Gravell | last post by:
I would like to know how to write this in LINQ You don't need LINQ for this; you can sort the existing list using a Comparer<T>: List<InterfaceBaselist = new List<InterfaceBase>(); ...
4
by: =?Utf-8?B?SkI=?= | last post by:
Hello List<Tis said to be more powerful than ArrayLists but if you have something like this: List<intmylst = new List<>; myList.Add("Joe"); myList.Add(25); the list doesn't seem to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.