Sorting an arrayList object

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# ?
Thanks in advance
LVL 1
hscanlanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
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
_kiwi_Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hscanlanAuthor Commented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

AlexFMCommented:
I think it should be

if(result != 0) return result;
0
hscanlanAuthor Commented:
Thanks that makes sense it did it :)
0
AlexFMCommented:
I think you could split points in this case.
0
Chester_M_RagelCommented:
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
_kiwi_Commented:
Hey all,

Gosh I'm tired lately..

Of course you had to read if(result != 0) return result;

Thanks Alex...
0
hscanlanAuthor Commented:
Hi yes i am sorry  :( i am new  now i know how to split them :)
0
AlexFMCommented:
Post request to Community Support:
http://www.experts-exchange.com/Community_Support/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.