Grouping items in a listbox and then sorting them

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 9418 Views - Last Post: 11 August 2012 - 10:42 PM Rate Topic: -----

#1 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 08:56 AM

Hi

I have a question, I need to sort out items in a list box which I know how to do but the problem is, my listbox is sorted out like this
firstname
surname
date
gender
address
and that is for one person and the next person is set out the same, what I want is to keep each of the person's 5 details together and sort them by date, but when I do that at the moment everyone's details get mixed, is it possible to keep a person's details together and sort everyone separate, I hope that makes sense.

Dim arr As New ArrayList
        Dim iLp As Integer

        arr.AddRange(lstFDisplay.Items)

        arr.Sort()

        lstFDisplay.Items.Clear()

        For iLp = 0 To arr.Count - 1
            lstFDisplay.Items.Add(arr(iLp))
        Next iLp


Is This A Good Question/Topic? 0
  • +

Replies To: Grouping items in a listbox and then sorting them

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 09:14 AM

How does the data come in - database? If so use the sql query to group and sort then assign that to a DataGridView's dataSource which handles the layout for you. If this is a custom class with properties you can do the same with LINQ and setting the DataGridView's dataSource to the LINQ query.
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 09:28 AM

View Postreggina, on 07 August 2012 - 04:56 PM, said:

Hi

I have a question, I need to sort out items in a list box which I know how to do but the problem is, my listbox is sorted out like this
firstname
surname
date
gender
address


Could clarify this? From this I took you to mean the follow
(0) Person 1 firstname
(1) Person 1 surname
(2) Person 1 date
(3) Person 1 gender
(4) Person 1 address
(5) Person 2 firstname
(6) Person 2 surname
(7) Person 2 date
(8) Person 2 gender
(9) Person 2 address


where the number in the brackets is the index position in the list.
Was This Post Helpful? 0
  • +
  • -

#4 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 07:12 PM

Yes that is correct Adam, its exactly like that.

View PostAdamSpeight2008, on 07 August 2012 - 09:28 AM, said:

View Postreggina, on 07 August 2012 - 04:56 PM, said:

Hi

I have a question, I need to sort out items in a list box which I know how to do but the problem is, my listbox is sorted out like this
firstname
surname
date
gender
address


Could clarify this? From this I took you to mean the follow
(0) Person 1 firstname
(1) Person 1 surname
(2) Person 1 date
(3) Person 1 gender
(4) Person 1 address
(5) Person 2 firstname
(6) Person 2 surname
(7) Person 2 date
(8) Person 2 gender
(9) Person 2 address


where the number in the brackets is the index position in the list.

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 07:32 PM

Then it's in my opinion a bad design and will cause you more problem.
As you'll have implement you're own sorting algorithm which takes into account the layout.See similar example

Define a custom class or classes. It'll make things a lot easier.
Spoiler

This post has been edited by AdamSpeight2008: 07 August 2012 - 07:32 PM

Was This Post Helpful? 1
  • +
  • -

#6 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 07 August 2012 - 07:53 PM

I totally agree with you Adam about how bad the design is, I have been arguing with the boss about it and telling him it causes more problems but he won't listen, so I am stuck with it unfortunately. I think this may be getting a little too advanced for me but I will have a go at it. So I need to create a custom class you say. Thanks for the link.

View PostAdamSpeight2008, on 07 August 2012 - 07:32 PM, said:

Then it's in my opinion a bad design and will cause you more problem.
As you'll have implement you're own sorting algorithm which takes into account the layout.See similar example

Define a custom class or classes. It'll make things a lot easier.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 08 August 2012 - 09:02 AM

I had a go at making a class but I don't think I did a very good job Adam, could you help me out and identify where I have gone wrong, thanks.

Public Enum applicationdatesort
    Female
    Male
End Enum

Public Class Person
    Public ReadOnly firstname As firstName
    Public ReadOnly lastname As lastname
    Public ReadOnly gender As gender
    Public ReadOnly applicationdate As applicationdate
    Public ReadOnly address As address
End Class

Public Class 
    MustInherit firstname
End Class
Public Class
    MustInherit lastname
End Class
Public Class
    MustInherit gender
End Class
Public Class
    MustInherit applicationdate
End Class
Public Class
    MustInherit address
End Class



Public Class appdate
    Inherits applicationdate
End Class
Public Class givenname
    Inherits firstName
End Class
Public Class surname
    Inherits lastname
End Class

Public Class maleorfemale
    Inherits gender
End Class

Public Class location
    Inherits address
End Class

Public Class appdatesort
    Inherits appdate
    Public ReadOnly firstName As String
    Public Sub New(ByVal firstname As String, ByVal lastname As String, ByVal gender As String, ByVal address As String)
        Me.appdate = appdate
    End Sub
End Class






Was This Post Helpful? 0
  • +
  • -

#8 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 07:23 AM

I created my custom class and I think I did a much better job of it this time, compared to the one above, I am now confused as how to use it to sort the listbox.
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 09:03 AM

Can you show us what your class now? And how you load it with data.
Was This Post Helpful? 1
  • +
  • -

#10 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 09:07 AM

To be honest I am not sure if it is correct and how exactly to use it to sort the listbox, I have been trying to read up on it now.

Public Class Person
    Public Property firstname As String
    Public Property lastname As String
    Public Property gender As String
    Public Property applicationdate As String
    Public Property address As String
    Public Sub New(ByVal firstname As String, Optional ByVal lastname As String = "None", Optional ByVal gender As String = "None", Optional ByVal applicationdate As String = "None", Optional ByVal address As String = "None")
        _firstname = firstname
        _lastname = lastname
        _gender = gender
        _applicationdate = applicationdate
        _address = address
    End Sub


View Post_HAWK_, on 09 August 2012 - 09:03 AM, said:

Can you show us what your class now? And how you load it with data.

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 10:15 AM

Why is everything of type string?
applicationdate is a Date, or even better as a specific custom class
Eg
Public Class ApplicationDate
  Public ReadOnly Date As DateTime
  Public Sub New(AppDate As DateTime)
    Me.Date = AppDate
  End Sub
  
  Public Overrides Function ToString() As String
    Return String.Format("{0}",Me.Date)
  End Function 
End Class


Was This Post Helpful? 1
  • +
  • -

#12 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 08:52 PM

Thanks Adam, for clearing that up for me, I am unsure of how to use the classes now to sort out the listbox, I have been research that subject but I can't find anything that makes sense to me.
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 10:45 PM

An example...

Your class:
Public Class Person
    Public Property firstname As String
    Public Property lastname As String
    Public Property gender As String
    Public Property applicationdate As Date
    Public Property address As String
    Public Sub New(ByVal firstname As String, Optional ByVal lastname As String = "None", Optional ByVal gender As String = "None", Optional ByVal applicationdate As Date = Nothing, Optional ByVal address As String = "None")
      _firstname = firstname
      _lastname = lastname
      _gender = gender
      _applicationdate = applicationdate
      _address = address
    End Sub
    'required for the listbox to know what to display
    'when it holds class objects
    Public Overrides Function ToString() As String
      Return String.Format("{0}, {1}, {2}", Me.lastname, Me.firstname, Me.applicationdate)
    End Function
End Class


Usage:
Private people As New List(Of Person)

  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
 people.Add(New Person("sue", "jenkins", "male", #1/2/2012#))
    people.Add(New Person("bob", "jenkins", "male", #1/2/2012#))
    people.Add(New Person("adam", "booth", "male", #1/20/2012#))
    people.Add(New Person("don", "rogers", "male", #1/20/2012#))
    people.Add(New Person("bob", "arnet", "male", #1/2/2012#))
    'order by lastname then by first name (if last is same) 
    'then group by date
    Dim query = people.OrderBy(Function(p) p.lastname).OrderBy(Function(p) p.firstname).GroupBy(Function(p) p.applicationdate)

    For Each qrp In query
      For Each item In qrp
        lsb.Items.Add(item)
      Next
    Next

  End Sub

Was This Post Helpful? 1
  • +
  • -

#14 reggina   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Grouping items in a listbox and then sorting them

Posted 09 August 2012 - 11:24 PM

Thanks Hawk for the eg. I understand the Class part of the code, but I am a little confused about the Usage part, for eg. can I use that on the sort button to sort out my listbox's.
Was This Post Helpful? 0
  • +
  • -

#15 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Grouping items in a listbox and then sorting them

Posted 10 August 2012 - 07:03 AM

Load the original list into listbox -> sort button -> clear items then use the method I showed you for the sort.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2