?
Solved

Sorting an arrayList

Posted on 2013-01-08
2
Medium Priority
?
227 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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