[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Sorting an arrayList object

Posted on 2004-11-12
10
Medium Priority
?
1,176 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:hscanlan
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12567861
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

by:
_kiwi_ earned 2000 total points
ID: 12568376
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

by:hscanlan
ID: 12568606
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 12568639
I think it should be

if(result != 0) return result;
0
 
LVL 1

Author Comment

by:hscanlan
ID: 12568707
Thanks that makes sense it did it :)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12568753
I think you could split points in this case.
0
 
LVL 6

Expert Comment

by:Chester_M_Ragel
ID: 12572327
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

by:_kiwi_
ID: 12573754
Hey all,

Gosh I'm tired lately..

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

Thanks Alex...
0
 
LVL 1

Author Comment

by:hscanlan
ID: 12681604
Hi yes i am sorry  :( i am new  now i know how to split them :)
0
 
LVL 48

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question