Solved

Sorting an arrayList

Posted on 2013-01-08
2
225 Views
Last Modified: 2013-01-09
I have an arrayList where each object has an int and a float.
I populate this list from data pairs and I wish to sort the arrayList by the floating point value field. The purpose is enable me to locate the associated int values for each of the sorted floats.

Am I using the correct data structure or is there a better alternative?
GM.
0
Comment
Question by:ManionG
[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
2 Comments
 
LVL 4

Accepted Solution

by:
Rahul Gupta earned 500 total points
ID: 38757807
0
 
LVL 9

Expert Comment

by:sognoct
ID: 38758196
Here there is an example (I used array of object instead of array list) , you didn't say if you are working on vb.net or csharp ... so I wrote the example in vb for csharp version just ask ...

Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click
    Dim o() As myObject = New myObject() {New myObject(1, 9.2), New myObject(2, 8.223), New myObject(4, 10.56), New myObject(5, 5.73)}
    Dim com As New clsInfoComparer
    Array.Sort(o, com)
    For Each m As myObject In o
      Console.WriteLine(m.val1.ToString & " " & m.val2.ToString)
    Next

  End Sub

  Public Class myObject
    Public val1 As Int32
    Public val2 As Double
    Public Sub New(ByVal v1 As Int32, ByVal v2 As Double)
      val1 = v1
      val2 = v2
    End Sub
  End Class

  Public Class clsInfoComparer
    Implements IComparer

    Function Compare(ByVal x As [Object], ByVal y As [Object]) As Integer Implements IComparer.Compare
      Dim t1, t2 As myObject
      t1 = CType(x, myObject)
      t2 = CType(y, myObject)
      If (t1.val2 > t2.val2) Then
        Return 1
      End If

      If (t1.val2 < t2.val2) Then
        Return -1
      Else
        Return 0
      End If
    End Function 'IComparer.Compare
  End Class 'myReverserClass

Open in new window

0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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