?
Solved

.NET 2005 Parallel ArrayLists - Sorting

Posted on 2007-04-01
9
Medium Priority
?
249 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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