Solved

Sorting an arrayList

Posted on 2013-01-08
2
202 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
2 Comments
 
LVL 4

Accepted Solution

by:
rahulgupta9x 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

11 Experts available now in Live!

Get 1:1 Help Now