3 Replies - 209 Views - Last Post: 22 January 2018 - 03:41 AM Rate Topic: -----

#1 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 164
  • Joined: 28-February 14

Sort Class Array by Integer

Posted 21 January 2018 - 07:37 PM

I made a class with properties Integer and Text. I'm trying to sort the list by the Integer.
List.Add(New iList(100, "test 1"))
List.Add(New iList(101, "test 2"))
List.Add(New iList(13, "test 3"))

Dim sorted = List.OrderBy(Function(x) x.Value)

Using the code above, it returns "test 3" last even though it's the lowest integer. How do I get it sort it properly?

Is This A Good Question/Topic? 0
  • +

Replies To: Sort Class Array by Integer

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 14038
  • View blog
  • Posts: 56,176
  • Joined: 12-June 08

Re: Sort Class Array by Integer

Posted 21 January 2018 - 08:37 PM

No.. it's working as expected. 13 is the lowest integer.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6544
  • View blog
  • Posts: 26,526
  • Joined: 12-December 12

Re: Sort Class Array by Integer

Posted 22 January 2018 - 02:29 AM

Are List and iList names of your own custom types? The first thing I suggest you do is to rename them, they are already the names of existing .NET types.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1492
  • View blog
  • Posts: 3,786
  • Joined: 01-February 13

Re: Sort Class Array by Integer

Posted 22 January 2018 - 03:41 AM

Is "Value" actually the name of an Integer property in your class? If it is, then the code you posted should do what you say you want.

Just for an example, i threw this together which should mimic your setup. It orders them correctly for me. Button1 orders them in ascending order and Button 2 orders them in a descending order.
Public Class Form1
    Private exList As New List(Of ExampleClass)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        exList.Add(New ExampleClass(100, "Test 1"))
        exList.Add(New ExampleClass(101, "Test 2"))
        exList.Add(New ExampleClass(13, "Test 3"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sortedList As List(Of ExampleClass) = exList.OrderBy(Function(x) x.Value).ToList
        RichTextBox1.Clear()
        For Each ex As ExampleClass In sortedList
            RichTextBox1.AppendText(ex.Name & "  " & ex.Value.ToString & Environment.NewLine)
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim sortedList As List(Of ExampleClass) = exList.OrderByDescending(Function(x) x.Value).ToList
        RichTextBox1.Clear()
        For Each ex As ExampleClass In sortedList
            RichTextBox1.AppendText(ex.Name & "  " & ex.Value.ToString & Environment.NewLine)
        Next
    End Sub
End Class

Public Class ExampleClass
    Public Property Name As String
    Public Property Value As Integer

    Public Sub New(exValue As Integer, exName As String)
        Value = exValue
        Name = exName
    End Sub
End Class


Button1 Results:
Test 3 13
Test 1 100
Test 2 101

Button2 Results:
Test 2 101
Test 1 100
Test 3 13
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1