Solved

.NET 2005 Parallel ArrayLists - Sorting

Posted on 2007-04-01
9
219 Views
Last Modified: 2013-11-26
I have two ArrayLists in a .NET 2005 class. One stores a list of candidates. The other, the number of votes that they have. I'm trying to now write a function that will return a formatted result for the number of votes each candidate has. Obviously I could leave them in the order in which they were input (which I suppose would work okay), but it seems logical to want to put the winner at the top with the number of votes they received and a percentage. How can I sort both lists so that each entry winds up staying in the same position as the corresponding number of votes?
0
Comment
Question by:carter_jl
  • 5
  • 3
9 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18833864
This just begs the question...

Why is the data in two different ArrayLists?

Put that data TOGETHER into a custom class that implements IComparer:
http://msdn2.microsoft.com/en-us/library/system.collections.icomparer.aspx

0
 

Author Comment

by:carter_jl
ID: 18834127
My bad on the wrong TA.

Point taken on writing a custom class. I'm still not sure if I want to do that since it won't really be reusable in other "Election"-type classes I'll be writing. However, I don't understand how the IComparer helps me at all.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837105
Unfortunately I don't code in C++ (I think that's what you're using?)...

Here is an example in VB.Net, hope you can make sense of it.

Output:
Avatar with 500 vote(s)
Danny Phantom with 100 vote(s)
Jimmy Neutron with 75 vote(s)
SpongeBob with 1 vote(s)

Public Class Form1

    Public Class VotingResults
        Implements IComparable

        Public Candidate As String
        Public Votes As Integer

        Public Sub New(ByVal Candidate As String, ByVal Votes As Integer)
            Me.Candidate = Candidate
            Me.Votes = Votes
        End Sub

        Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
            Dim vr As VotingResults = CType(obj, VotingResults)
            Return vr.Votes.CompareTo(Me.Votes)
        End Function

    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim results As New ArrayList
        results.Add(New VotingResults("SpongeBob", 1))
        results.Add(New VotingResults("Danny Phantom", 100))
        results.Add(New VotingResults("Avatar", 500))
        results.Add(New VotingResults("Jimmy Neutron", 75))
        results.Sort()

        For Each vr As VotingResults In results
            Debug.Print(vr.Candidate & " with " & vr.Votes & " vote(s)")
        Next
    End Sub

End Class
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837176
*I'm acutally using IComparable instead of IComparer...
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:carter_jl
ID: 18840320
I can make some sense of that, but since I'm relatively new to .NET (especially C++ CLI), I'm unsure of how to translate that to C++
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18840328
I can translate to C# if you like...don't know how much that would help you....
0
 

Author Comment

by:carter_jl
ID: 18840415
Might be worth a shot
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18840630
Here is the C# version:

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private class VotingResults : IComparable
        {

            public string Candidate;
            public int Votes;

            public VotingResults(string Candidate, int Votes)
            {
                this.Candidate = Candidate;
                this.Votes = Votes;
            }

            public int CompareTo(object obj)
            {
                VotingResults vr = (VotingResults)obj;
                return vr.Votes.CompareTo(this.Votes);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ArrayList results = new ArrayList();
            results.Add(new VotingResults("SpongeBob", 1));
            results.Add(new VotingResults("Danny Phantom", 100));
            results.Add(new VotingResults("Avatar", 500));
            results.Add(new VotingResults("Jimmy Neutron", 75));
            results.Sort();

            foreach (VotingResults vr in results)
            {
                Debug.Print(vr.Candidate + " with " + vr.Votes + " vote(s)");
            }              
        }

    }
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now