Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to sort the following list?

How to sort the following list?

Scheduled Pinned Locked Moved Visual Basic
data-structurestutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Milad Biroonvand
    wrote on last edited by
    #1

    How to sort the following list, nodes is my list tv is my treeview Now I'd like nodes to sort.

    Dim nodes As New List(Of TreeNode)
    Dim queue As New Queue(Of TreeNode)
    Dim top As TreeNode
    Dim nod As TreeNode
    For Each top In Tv.Nodes
    queue.Enqueue(top)
    Next
    While (queue.Count > 0)
    top = queue.Dequeue
    nodes.Add(top)
    For Each nod In top.Nodes
    queue.Enqueue(nod)
    Next
    End While

    Thanks!

    L 1 Reply Last reply
    0
    • M Milad Biroonvand

      How to sort the following list, nodes is my list tv is my treeview Now I'd like nodes to sort.

      Dim nodes As New List(Of TreeNode)
      Dim queue As New Queue(Of TreeNode)
      Dim top As TreeNode
      Dim nod As TreeNode
      For Each top In Tv.Nodes
      queue.Enqueue(top)
      Next
      While (queue.Count > 0)
      top = queue.Dequeue
      nodes.Add(top)
      For Each nod In top.Nodes
      queue.Enqueue(nod)
      Next
      End While

      Thanks!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Why do you use a Queue? the main purpose of a queue is to act as a pipe where things get added on one side, stored, and removed on the other side, which inherently means their order is always preserved. What you need is another kind of collection, such as a simple List, on which you can call Sort(). Then provide a Comparer to explain how you compare two TreeNode instances. This[^] explains and illustrates it. :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups