Solved

.NET 2005 Parallel ArrayLists - Sorting

Posted on 2007-04-01
9
206 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 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

12 Experts available now in Live!

Get 1:1 Help Now