Solved

.NET 2005 Parallel ArrayLists - Sorting

Posted on 2007-04-01
9
247 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

691 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