Solved

# Sorting an arrayList object

Posted on 2004-11-12
1,128 Views
Hello Expert, I have an arrayList in it I have
LastName, Firstname, Age
What I want to do is sort first LastName then firstName then age  an example is
Smith John 12
Smith John 14
Smith Karri 10
Smith karri 40
Timberson Mike 20
etc.
Is there a way to do this in C# ?
0
Question by:hscanlan

LVL 48

Expert Comment

Use ArrayList.Sort function.

IComparer myComparer = new MyComparer();
myList.Sort( myComparer );

public class MyComparer : IComparer
{
// This function should return value:
// Less than zero, if x is less than y
// Zero, if x equals y
// Greater than zero, if x is greater than y.
int IComparer.Compare( Object x, Object y )
{
// cast x and y to your class type and compare two instances
}
}

0

LVL 3

Accepted Solution

Just develop a custom comparer, as AlexFM said, with, for the comparison, something like: (I assume the values in a string are separated with ',')

public class MyComparer : IComparer
{
// This function should return value:
// Less than zero, if x is less than y
// Zero, if x equals y
// Greater than zero, if x is greater than y.
int IComparer.Compare( Object x, Object y )
{
string[] list1 = ((string) x).Split(',');
string[] list2= ((string) y).Split(',');

CaseInsensitiveComparer comparer = new CaseInsensitiveComparer();

// Compare last names
int result = comparer.compare(list1[0], list2[0]);
if(result != 0) return 0;

// Compare first names
result = comparer.compare(list1[1], list2[1]);
if(result != 0) return 0;

// Compare ages
return (int.Parse(list1[2]) - int.Parse(list2[2]));
}
}
0

LVL 1

Author Comment

I tried the following i and it seems not to work ?
public class SMLSort : IComparer
{
public SMLSort(): base()
{

}

int IComparer.Compare(object x, object y)
{

PanelControlSheet.ImportSMLPD aSMLx = (PanelControlSheet.ImportSMLPD) x;
PanelControlSheet.ImportSMLPD aSMLY = (PanelControlSheet.ImportSMLPD) y;

CaseInsensitiveComparer comparer = new CaseInsensitiveComparer();

// Compare last names
int result = comparer.Compare(aSMLx.Last, aSMLY.Last);
if(result != 0) return 0;

// Compare first names
result = comparer.Compare(aSMLx.First, aSMLY.First);
if(result != 0) return 0;

// Compare ages
return result = comparer.Compare(aSMLx.Age,aSMLY.Age);

}

}
0

LVL 48

Expert Comment

I think it should be

if(result != 0) return result;
0

LVL 1

Author Comment

Thanks that makes sense it did it :)
0

LVL 48

Expert Comment

I think you could split points in this case.
0

LVL 6

Expert Comment

Yep Alex.. you are correct. I think hscanlan is very new to this place.

Hi hscanlan, can you pls read this the help and guideline?

http://www.experts-exchange.com/help.jsp
0

LVL 3

Expert Comment

Hey all,

Gosh I'm tired lately..

Thanks Alex...
0

LVL 1

Author Comment

Hi yes i am sorry  :( i am new  now i know how to split them :)
0

LVL 48

Expert Comment

Post request to Community Support:
http://www.experts-exchange.com/Community_Support/
0

## Featured Post

### Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…