locked
for loop in descending order RRS feed

  • Question

  • Hello,

    I have an itemsList and I'm able to use for loop normally like

    for each listItem in itemsList
    
    console.writeline(listItem.ID)
    
    Next

    Prints out 1, 2, 3 ...

    ItemsList contains items in the order 1, 2, 3

    But how do I use for each in descending order? so, it prints out 4,3,2,1

    Tuesday, March 13, 2018 1:45 PM

All replies

  •        For i = Itemslist.Count - 1 To 0 Step -1
                messagebox.show(ItemsList(i).ID)
            Next



    Success
    Cor


    • Edited by Cor Ligthert Tuesday, March 13, 2018 1:55 PM
    • Proposed as answer by leshay Tuesday, March 13, 2018 2:19 PM
    Tuesday, March 13, 2018 1:55 PM
  • Hello,

    Perhaps this would work for you

    Dim names As New List(Of String) From {"Karen", "Bill", "Mary"}
    For Each name As String In names.ToArray.Reverse
        Console.WriteLine(name)
    Next

    And

    Dim values As New List(Of Integer) From {1, 2, 3, 4}
    For Each name As Integer In values.ToArray.Reverse
        Console.WriteLine(name)
    Next

    Same for a class

    Public Class Person
        Public Property Id As Integer
        Public Property Name As String
    End Class

    Demo

    Dim people As New List(Of Person) From
        {
            New Person With {.Id = 1, .Name = "Bob Smith"},
            New Person With {.Id = 2, .Name = "Jean Doe"}
        }
    For Each person As Person In people.ToArray.Reverse
        Console.WriteLine(person.Id)
    Next

    Or

    Dim peopleReverseOrder = From p In people Order By p.Id Descending
    For Each peep In peopleReverseOrder
        Console.WriteLine(peep.Id)
    Next


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Tuesday, March 13, 2018 1:55 PM
  • Karen, 

    Not that it is better than a reverse index loop, was I interested if the ToArray was a deep copy array or a simple shallow array. It is the later and therefore it can also be used as reference.  

    I did not test if it can be used for deleting with a for loop, for what the reverse loop is mostly needed. That you may do. 


    Success
    Cor

    Tuesday, March 13, 2018 2:14 PM
  • Hello,

    I have an itemsList and I'm able to use for loop normally like

    for each listItem in itemsList
    
    console.writeline(listItem.ID)
    
    Next

    Prints out 1, 2, 3 ...

    ItemsList contains items in the order 1, 2, 3

    But how do I use for each in descending order? so, it prints out 4,3,2,1

    Using LINQ the For Each can be manipulated to provide the order.  Here is an example:

        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            'create test data
            Dim names As New List(Of String) From {"Mary", "Karen", "Bill", "Martha", "Frank"}
            Dim itemsList As New List(Of Test)
            For idx As Integer = 0 To names.Count - 1
                Dim foo As New Test(names(idx), idx + 1)
                itemsList.Add(foo)
            Next
            Debug.WriteLine("")
    
            Stop
            For Each item As Test In itemsList.OrderByDescending(Function(i) i.id) 'descending id
                Debug.WriteLine("{0}  {1}", item.id, item.name)
            Next
    
            Debug.WriteLine("")
    
            Stop
            For Each item As Test In itemsList.OrderBy(Function(i) i.name) 'name
                Debug.WriteLine("{0}  {1}", item.id, item.name)
            Next
    
            Debug.WriteLine("")
    
            Stop
            For Each item As Test In (From t In itemsList Where t.name.StartsWith("M") Select t Order By t.name)
                'certain names
                Debug.WriteLine("{0}  {1}", item.id, item.name)
            Next
        End Sub
    
        Public Class Test
            Property name As String
            Property id As Integer
    
            Public Sub New(aName As String, anID As Integer)
                Me.name = aName
                Me.id = anID
            End Sub
        End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Tuesday, March 13, 2018 4:12 PM
  • But how do I use for each in descending order? so, it prints out 4,3,2,1

    If this is a task that you have been assigned then the solution that is expected is to use a custom iterator.   See the example under Iterators here:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-each-next-statement

    The task is to convert that example from Evens to Reverse.

    Tuesday, March 13, 2018 10:06 PM
  • Hi Brian,

    You can also take a look the following code:

    Dim list As New List(Of String) From {"Cherry", "Annian", "Mattew", "Barry"}
            Dim q = From i In list Order By i Descending
                    Select i
    
      Dim list1 As New List(Of test1) From {New test1 With {.Id = 1, .Name = "Cherry"},
            New test1 With {.Id = 1, .Name = "Annian"},
            New test1 With {.Id = 3, .Name = "Mattew"},
            New test1 With {.Id = 4, .Name = "Barry"}}
    
            list1 = list1.OrderByDescending(Function(x) x.Name).ToList()
            For Each item In list1
                Console.WriteLine(item.Name)
            Next
    
    Public Class test1
        Public Property Id As Integer
        Public Property Name As String
    
    End Class

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 14, 2018 2:29 AM
  • One final go at it.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'create test data
            Dim names As New List(Of String) From {"Mary", "Karen", "Bill", "Martha", "Frank"}
            Dim itemsList As New List(Of Test)
            Dim rslts As List(Of Test)
            For idx As Integer = 0 To names.Count - 1
                Dim foo As New Test(names(idx), idx + 1)
                itemsList.Add(foo)
            Next
    
            Dim slctr As IEnumerable = itemsList.OrderByDescending(Function(i) i.id)
            rslts = FilterItems(slctr)
    
            slctr = itemsList.OrderBy(Function(i) i.name)
            rslts = FilterItems(slctr)
    
            slctr = (From t In itemsList Where t.name.StartsWith("M") Select t Order By t.name)
            rslts = FilterItems(slctr)
    
        End Sub
    
        Public Function FilterItems(Selector As IEnumerable) As List(Of Test)
            Dim rv As New List(Of Test)
            Debug.WriteLine("")
            For Each item As Test In Selector
                rv.Add(item)
                Debug.WriteLine("{0}  {1}", item.id, item.name)
            Next
            Debug.WriteLine("")
            Return rv
        End Function
    
        Public Class Test
            Property name As String
            Property id As Integer
    
            Public Sub New(aName As String, anID As Integer)
                Me.name = aName
                Me.id = anID
            End Sub
        End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Friday, March 16, 2018 4:41 PM