Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.NET 2005 Parallel ArrayLists - Sorting

Posted on 2007-04-01
9
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 86

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 86

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18837176
*I'm acutally using IComparable instead of IComparer...
0
 

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 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

636 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